[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] using for-each-group to wrap an element


Subject: [xsl] using for-each-group to wrap an element
From: Jeff Wilson <jeffrey@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 23 Aug 2011 02:08:38 -0700

Good morning,

I'm still working with for-each-group in order to wrap an element around adjacent siblings. I decided to use a 2nd transformation to take care of the grouping. I'm getting closer, but I don't know what I've done wrong here.

Again, thank you for your help.

Input:
<root>
<ChapterTitle>...</ChapterTitle>
<H1>...</H1>
<Para>...</Para>
<Para>...</Para>
<AnchoredGraphic><img src="..."/></AnchoredGraphic>
<H1>...</H1>
<Para>...</Para>
<H2>...</H2>
<Para>...</Para>
<FeatureType>...</FeatureType>
<FeaturePara>...</FeaturePara>
<ListNumbered>...</ListNumbered>
<ListNumbered>...</ListNumbered>
<ListPara>...</ListPara>
<ListNumbered>...</ListNumbered>
</root>

Desired Output:
<root>
<ChapterTitle>...</ChapterTitle>
<H1>...</H1>
<Para>...</Para>
<Para>...</Para>
<AnchoredGraphic><img src="..."/></AnchoredGraphic>
<H1>...</H1>
<Para>...</Para>
<H2>...</H2>
<Para>...</Para>
<AnchoredFeature>
<FeatureType>...</FeatureType>
<FeaturePara>...</FeaturePara>
</AnchoredFeature
<ListNumbered>...</ListNumbered>
<ListNumbered>...</ListNumbered>
<ListPara>...</ListPara>
<ListNumbered>...</ListNumbered>
</root>

Current output:
<root>
<ChapterTitle>...</ChapterTitle>
<H1>...</H1>
<Para>...</Para>
<Para>...</Para>
<AnchoredGraphic><img src=".."/></AnchoredGraphic>
<H1>...</H1>
<Para>...</Para>
<H2>...</H2>
<Para>...</Para>
<AnchoredFeature><FeatureType>...</FeatureType></AnchoredFeature>
<AnchoredFeature><FeaturePara>...</FeaturePara></AnchoredFeature>
<ListNumbered>...</ListNumbered>
<ListNumbered>...</ListNumbered>
<ListPara>...</ListPara>
<ListNumbered>...</ListNumbered>
</root>

XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="root">
<xsl:element name="root">
<xsl:for-each-group select="child::node()" group-adjacent="starts- with(name(),'Feature')">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:element name="AnchoredFeature">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:element>
</xsl:template>
</xsl:stylesheet>



Current Thread