Re: [xsl] grouping nodes inside a new parent node

Subject: Re: [xsl] grouping nodes inside a new parent node
From: Andrew Male <ccsajm@xxxxxxxxxx>
Date: Fri, 25 Feb 2005 15:25:23 +0000

Thanks for the information it helped a lot. Just in case anyone else was interested and to close this thread this is what is working for me. Perhaps it could be a little more elegant but....... I have also included the source of channel-list.xml which is used earlier in the xslt

NB. This:

<xsl:copy-of select=".|following-sibling::table|following-sibling::subheading|following-sibling::para|following-sibling::images|following-sibling::top|following-sibling::list"/>

was the only way I could think of saying every following sibling except <notes>. Explicitly saying all the following-siblings I want except <notes>. I notice XSLT 2.0 has provision for this, anything better than what I have used?

Working XSLT ------------

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""; xmlns:xsi="";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="contentnodes"/>
<xsl:template match="@xsi:noNamespaceSchemaLocation">
<xsl:attribute name="xsi:noNamespaceSchemaLocation"></xsl:attribute>
<xsl:template match="publish-to">
<xsl:for-each select="document('channel-list.xml')/channels/area">
<xsl:variable name="attname">
<xsl:value-of select="."/>
<xsl:when test="$attname = //publish-to/*">
<xsl:attribute name="{$attname}"><xsl:text>true</xsl:text></xsl:attribute>
<xsl:attribute name="{$attname}"><xsl:text>false</xsl:text></xsl:attribute>
<xsl:template match="para[position() > 1]|table|subheading|images|top|list"/>
<xsl:template match="notes">
<xsl:apply-templates select="child::*"/>
<xsl:template match="news-item/para[1]">
<xsl:copy-of select=".|following-sibling::table|following-sibling::subheading|following-sibling::para|following-sibling::images|following-sibling::top|following-sibling::list"/>
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()"/>

channel-list.xml ----------------

<?xml version="1.0" encoding="UTF-8"?>

--On 25 February 2005 12:27 +0000 David Carlisle <davidc@xxxxxxxxx> wrote:

It depends a bit how general you want it to be but basically you want to
have empty templates for all the elements that you want to group so the
"identity" template doesn't copy them to the wrong place, then have a
template for the first element in the group that copies all the group to
where you want.

so if the rule is that the special nodes are para children of news-item
that don't follow a table then you want

<xsl:template match="news-item/para[not(preceding-sibling::table)]"/>


<xsl:template match="news-item/para[1]" priority="10">

as with any grouping problem you can probably speed it up with keys,
although in this case I'd guess your news-items don't have hundreds of
children so it might not make so much difference.


________________________________________________________________________ This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. For more information on a proactive anti-virus service working around the clock, around the globe, visit: ________________________________________________________________________

------------------------------------------------- Andrew Male University of Bath Computing Services (BUCS) Ext: x4937 ccsajm@xxxxxxxxxx

Current Thread