0

Hi I have an xml document which look like that:

<a> <!-- Several nodes "a" with the same structure for children -->
    <b>12</b>
    <c>12</c>
    <d>12</d>
    <e>12</e>
    <f>12</f>
    <g>12</g>
</a>

I'm trying to obtain the following document using xslt 2.0

<a>
    <b>12</b>
    <c>12</c>
    <wrap>
        <d>12</d>        
        <e>12</e>
        <f>12</f>
        <g>12</g>
    </wrap>
</a>

I started my xsl file with

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

And changed it for several cases like replacing a string part, filter some nodes, etc. But I'm stuck with "selecting four consecutive nodes", any clue on how to achieve the wrapping?

2
  • Can you select the nodes to wrap, specifically? Is it always the same set (d,e,f,g)?
    – line-o
    May 24, 2013 at 9:52
  • This is always the same set with the same order (d,e,f,g)
    – Jeremy D
    May 24, 2013 at 9:53

2 Answers 2

2

If all your a elements are genuinely exactly the same structure then the simplest would be a brute-force

<xsl:template match="a">
  <xsl:copy>
    <xsl:apply-templates select="b | c" />
    <wrap>
      <xsl:apply-templates select="d | e | f | g" />
    </wrap>
  </xsl:copy>
</xsl:template>

or if you want to be a bit cleverer

    <wrap>
      <xsl:apply-templates select="* except (b | c)" />
    </wrap>

If you want to always "wrap" the last four child elements of a, then how about

<xsl:template match="a">
  <xsl:variable name="lastFour" select="*[position() &gt; (last() - 4)]" />
  <xsl:copy>
    <xsl:apply-templates select="* except $lastFour" />
    <wrap>
      <xsl:apply-templates select="$lastFour" />
    </wrap>
  </xsl:copy>
</xsl:template>
1
  • plus one for simplicity -- err, specificity
    – line-o
    May 25, 2013 at 8:57
1

With XSLT 2.0 you can also make use of for-each-group group-adjacent:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="a">
      <xsl:copy>
        <xsl:for-each-group select="*" group-adjacent="boolean(self::d | self::e | self::f | self::g)">
          <xsl:choose>
            <xsl:when test="current-grouping-key()">
              <wrap>
                <xsl:apply-templates select="current-group()"/>
              </wrap>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="current-group()"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.