black sowl
black sowl

Reputation: 195

Node grouping using XSLT version 1.0

I need some help on the xslt that I'm writing. Below is my source xml.

<document>
    <content1></content1>
    <content2></content2>
    <Br/>
    <content3></content3>
    <content4></content4>
    <Br/>
    <content5></content5>
    <content6></content6>
</document>

Here's the structure of the output I intend to create:

<document>
    <p>
        <content1></content1>
        <content2></content2>
    </p>
    <p>
        <content3></content3>
        <content4></content4>
    </p>
    <p>
        <content5></content5>
        <content6></content6>
    </p>
</document>

My question is, how do I group the contents and wrap it in a "<p>" tag whenever I see the "<Br>" tag?

Thanks!

Upvotes: 1

Views: 146

Answers (2)

Vincent Biragnet
Vincent Biragnet

Reputation: 2998

This XSLT 1.0 gives you the expected result :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="parag" match="document/*[not(self::Br)]" use="count(following-sibling::Br)"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="document/*[not(self::Br) and not(position()=1)]"/>
    <xsl:template match="Br|document/*[position()=1]">
        <p>
            <xsl:for-each select="key('parag',count(following-sibling::Br))">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>                    
                </xsl:copy>
            </xsl:for-each>
        </p>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Wayne
Wayne

Reputation: 60414

Using the Muenchian Method to group children of document by their first preceding Br:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:key name="byPosition" match="/document/*[not(self::Br)]"
             use="generate-id(preceding-sibling::Br[1])"/>
    <xsl:template match="@*|node()" name="identity" priority="-5">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/document/*[not(self::Br) and 
            generate-id()=generate-id(key('byPosition', 
                                generate-id(preceding-sibling::Br[1]))[1])]">
        <p><xsl:copy-of 
               select="key('byPosition', 
                            generate-id(preceding-sibling::Br[1]))"/></p>
    </xsl:template>
    <xsl:template match="/document/*" priority="-3"/>
</xsl:stylesheet>

Explanation: First, items are grouped in a key based on their first preceding Br element:

<xsl:key name="byPosition" match="/document/*[not(self::Br)]"
         use="generate-id(preceding-sibling::Br[1])"/>

The Identity Transform is used to copy most nodes through unchanged:

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

We then match all children of document that are the first such item for their key:

<xsl:template match="document/*[not(self::Br) and 
            generate-id()=generate-id(key('byPosition', 
                                generate-id(preceding-sibling::Br[1]))[1])]">

...and use that as the point at which to copy all items grouped by that key:

<xsl:copy-of select="key('byPosition', 
                          generate-id(preceding-sibling::Br[1]))"/>

Upvotes: 1

Related Questions