CoryO
CoryO

Reputation: 533

How to group elements based on their siblings using XSLT

I am trying to group several elements based on a starting and ending attribute of their surrounding siblings.

Sample XML:

<list>
  <item>One</item>
  <item class="start">Two</item>
  <item>Three</item>
  <item class="end">Four</item>
  <item>Five</item>
  <item class="start">Six</item>
  <item class="end">Seven</item>
  <item>Eight</item>
</list>

Desired Result:

<body>
  <p>One</p>
  <div>
    <p>Two</p>
    <p>Three</p>
    <p>Four</p>
  </div>
  <p>Five</p>
  <div>
    <p>Six</p>
    <p>Seven</p>
  </div>
  <p>Eight</p>
</body>

I come close to the desired results with the following XSLT. However, the following-sibling match doesn't stop after it reaches the ending attribute. Also, the standard matching repeats the elements that were already output from the following-sibling match.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="list">
        <body>
            <xsl:apply-templates />
        </body>
    </xsl:template>
    <xsl:template match="item">
        <p>
            <xsl:apply-templates />
        </p>
    </xsl:template>
    <xsl:template match="item[@class='start']">
        <div>
            <p><xsl:apply-templates /></p>
            <xsl:apply-templates select="following-sibling::*[not(preceding-sibling::*[1][@class='end'])]" />
        </div>
    </xsl:template>
</xsl:transform>

Upvotes: 0

Views: 390

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

Since you're using XSLT 2.0, why don't you take advantage of it:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/list">
    <body>
        <xsl:for-each-group select="item" group-starting-with="item[@class='start']">
            <xsl:for-each-group select="current-group()" group-ending-with="item[@class='end']">
                <xsl:choose>
                    <xsl:when test="count(current-group()) gt 1">
                        <div>
                            <xsl:apply-templates select="current-group()" />
                        </div>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="current-group()" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each-group>
        </xsl:for-each-group>
    </body>
</xsl:template>

<xsl:template match="item">
    <p>
        <xsl:value-of select="."/>
    </p>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions