José
José

Reputation: 573

Grouping some XML elements with XSLT

I am struggling with XSLT for some hours and I came to StackOverflow for help. I have some input text like this:

<?xml version="1.0" encoding="UTF-8"?>
<div>
<p class="text">Some regular text</p>
<p class="first">The first part</p>
<p class="next">and then some text</p>
<p class="next">and some more</p>

<p class="regular">Some regular text</p>
<p class="text1">Some regular text</p>
<p class="text2">Some regular text</p>
<p class="first">Here some new text</p>
<p class="next">and some more</p>
<p class="next">and the end of this section</p>
<p class="text3">Some regular text</p>
</div>

I would like to end up with this

<?xml version="1.0" encoding="UTF-8"?>
<div>
<p>Some regular text</p>

<group>
    <p>The first part</p>
    <p>and then some text</p>
    <p>and some more</p>
</group>

<p>Some regular text</p>
<p>Some regular text</p>
<p>Some regular text</p>

<group>
    <p>Here some new text</p>
    <p>and some more</p>
    <p>and the end of this section</p>
</group>

<p>Some regular text</p>
</div>

I know this is not amazingly difficult, but I don't get it... The XSLT I have got right now is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
    <xsl:apply-templates select="*" />
</xsl:template>

<xsl:template match="*">
    <xsl:copy>
        <xsl:for-each select="@*">
            <xsl:copy />
        </xsl:for-each>
        <xsl:apply-templates />
    </xsl:copy>
</xsl:template>

<xsl:template match="div">
    <lg>
        <xsl:apply-templates/>
    </lg>
</xsl:template>


<xsl:template match="/div">
    <xsl:for-each-group select="//p[@class='first']/following-sibling::p[@class='next']" group-by="text()">
        <group>
            <xsl:apply-templates/>
        </group>
    </xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>

That doesn't work at all :( I would be very grateful if anyone could help me, because I really have problems understanding how XSLT works...

Best regards, José

Upvotes: 1

Views: 75

Answers (1)

Tim C
Tim C

Reputation: 70598

You are trying to group the p which have a class attribute set to "first", followed by all the "next" ones.

For your given XML, this XSLT may do the trick:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />

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

  <xsl:template match="/div">
    <xsl:for-each-group select="p" group-adjacent="@class = 'first' or @class='next'">
        <xsl:choose>
            <xsl:when test="@class = 'first' or @class='next'">
                <group>
                    <xsl:apply-templates select="current-group()" />
                </group>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="current-group()" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="@class" />
</xsl:stylesheet>

However, this would not work in this case, as it would put all the p elements in one group.

<div>
<p class="first">The first part</p>
<p class="next">and then some text</p>
<p class="first">Here some new text</p>
<p class="next">and some more</p>
</div>

If this is not what you want, try this XSLT instead

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />

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

  <xsl:template match="/div">
    <xsl:for-each-group select="p" group-starting-with="p[@class='first']">
      <xsl:for-each-group select="current-group()" group-adjacent="@class='first' or @class='next'">
        <xsl:choose>
            <xsl:when test="@class='first' or @class='next'">
                <group>
                    <xsl:apply-templates select="current-group()" />
                </group>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="current-group()" />
            </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:for-each-group>
</xsl:template>

<xsl:template match="@class" />
</xsl:stylesheet>

Upvotes: 2

Related Questions