1

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

6
  • Your input isn't XML. You can't differentiate the texts. Commented Jul 15, 2014 at 8:36
  • "need code in xslt 1.0" Then you shouldn't be asking about XSLT 2.0, nor tagging your question as such. Commented Jul 15, 2014 at 8:58
  • @Kabulan0lak "Your input isn't XML." I am afraid you are very much mistaken about that. Commented Jul 15, 2014 at 9:00
  • "need code in xslt 1.0" - Not even saying "please" anymore, are we? Not that saying "please" would help much. Commented Jul 15, 2014 at 9:39
  • @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. Commented Jul 15, 2014 at 12:10

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible in xslt 1.0
Yes, but more difficult. Look up "sibling recursion". Sorry, I don't have time to write complex code for you in XSLT 1.0 when it can be done so easily in 2.0.
@LuvKS: see previous remark by Michael Kay: there are many posts on "sibling recursion", and alternatively, "Muenchian grouping". Or ask a new question (remember that in this question you said both xslt 1.0 and 2.0, so the obvious choice for this answer was 2.0, which has for-each-group instruction).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.