[xsl] grouping nodes inside a new parent node

Subject: [xsl] grouping nodes inside a new parent node
From: Andrew Male <ccsajm@xxxxxxxxxx>
Date: Fri, 25 Feb 2005 12:14:50 +0000
I am attempting to copy all elements in my source xml file to a new xml file using the identity transform. I have templates for a couple of elements to make changes but I am having a problem with one particular change I need to make.

I need to take a group of nodes (from the first para element to the table element in the source below) and make them children of a <content> node in the same position as the source.

I have attempted to match the first para in a template like this:

<template match="para[1]">
	<content>
		<xsl:apply-templates select="following-sibling::*"
	</content>
</template>

the problem is that it grabs everything including the <notes> element and puts it in the <content> element, plus the identity transform continues to process the nodes (except the para[1]) and outputs them after the </content>.

Can anyone advise me on the approach that I should be taking or guide me in the right direction? Thanks in advance.

Source xml
----------

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

<news-item xmlns:xs="http://www.w3.org/2001/XMLSchema"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:noNamespaceSchemaLocation="http://www.bath.ac.uk/news/schema/news.xsd";>
<publish-to>
<channel>business</channel>
<channel>international</channel>
<channel>research</channel>
</publish-to>
<internal-news>false</internal-news>
<related-links>
<linkgroup heading="test">
<link description="this is a test" uri="http://www.bath.ac.uk";>test</link>
</linkgroup>
</related-links>
<creation-date>2005-02-24</creation-date>
<release-date>2005-02-24T13:19:00</release-date>
<archive-date>2005-02-24T13:19:00</archive-date>
<contact-details>Please contact me<mailto email-address="ccsajm@xxxxxxxxxx">Mail Andy Male</mailto>thanskks</contact-details>
<heading>This is the headline</heading>
<summary>This is a summary of the article</summary>
<para>This is the main text</para>
<para>of the news item</para>
<para>with a couple of para's</para>
<table border="true" cellspacing="3">
<row>
<cell>text for the talbble</cell>
<cell>more text</cell>
<cell>more</cell>
</row>
</table>
<notes>
<para>test</para>
</notes>
</news-item>


XSLT
----

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
<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">http://www.bath.ac.uk/bucs/system/xxeconfig/config/news/newseditor.xsd</xsl:attribute>
</xsl:template>
<xsl:template match="publish-to">
<xsl:copy>
<xsl:for-each select="document('channel-list.xml')/channels/area">
<xsl:variable name="attname">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$attname = //publish-to/*">
<xsl:attribute name="{$attname}"><xsl:text>true</xsl:text></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{$attname}"><xsl:text>false</xsl:text></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


Snippet of desired output
-------------------------
.
.
.
	<heading>This is the headline</heading>
	<summary>This is a summary of the article</summary>
	<content>
		<para>This is the main text</para>
		<para>of the news item</para>
		<para>with a couple of para's</para>
		<table border="true" cellspacing="3">
			<row>
				<cell>text for the talbble</cell>
				<cell>more text</cell>
				<cell>more</cell>
			</row>
		</table>
		<notes>
			<para>test</para>
		</notes>
	</content>
</news-item>


Andrew Male


Current Thread