1
votes

How to convert below xml to given output using xslt 1.0 and xslt 2.0. Please refer to given input and output............. Input:-

<block>
    <line>
        <formatting>
            <word>text 1</word>
        </formatting>
        <formatting bold="1">
            <word>text 2</word>
        </formatting>
        <formatting bold="1">
            <word>text 3</word>
        </formatting>
        <formatting bold="1">
            <word>text 4</word>
        </formatting>
        <formatting>
            <word>text 5</word>
        </formatting>
        <formatting bold="1">
            <word>text 6</word>
        </formatting>
        <formatting bold="1">
            <word>text 7</word>
        </formatting>
        <formatting>
            <word>text 8</word>
        </formatting>
        <formatting>
            <word>text 9</word>
        </formatting>
        <formatting bold="1">
            <word>text 10</word>
        </formatting>
        <formatting>
            <word>text 11</word>
        </formatting>
    </line>
</block>

Output should be:-

<p>text 1 <b>text 2 text 3 text 4</b> text 5 <b>text 6 text 7</b> text 8 text 9 <b>text 10</b> text 11</p>

need code in xslt 1.0

1
Your input isn't XML. You can't differentiate the texts. - Kabulan0lak
"need code in xslt 1.0" Then you shouldn't be asking about XSLT 2.0, nor tagging your question as such. - michael.hor257k
@Kabulan0lak "Your input isn't XML." I am afraid you are very much mistaken about that. - michael.hor257k
"need code in xslt 1.0" - Not even saying "please" anymore, are we? Not that saying "please" would help much. - Tomalak
@michael.hor257k lol the input wasn't like this when I wrote my comment. It was not xml : " text1 text2 text2 ... " but now it is. - Kabulan0lak

1 Answers

2
votes

With XSLT 2.0 and an XSLT 2.0 processor like Saxon 9 you can use for-each-group group-adjacent:

<xsl:stylesheet 
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:template match="line">
  <p>
    <xsl:for-each-group select="formatting" group-adjacent="string(@bold)">
      <xsl:if test="position() gt 1"><xsl:text> </xsl:text></xsl:if>
      <xsl:choose>
        <xsl:when test="current-grouping-key() = '1'">
          <b>
            <xsl:value-of select="current-group()/word"/>
          </b>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="current-group()/word"/>
        </xsl:otherwise>

      </xsl:choose>
    </xsl:for-each-group>
  </p>
</xsl:template>

</xsl:stylesheet>