[xsl] merging duplicate consecutive elements

Subject: [xsl] merging duplicate consecutive elements
From: "Annmarie Rubin \(anrubin\)" <anrubin@xxxxxxxxx>
Date: Tue, 18 Oct 2005 18:25:04 -0700
Hello list,

I have tried the following xslt to try to concatenate the contents of
duplicate, consecutive info elements in xml, but it writes the input
tree to the result tree without merging the duplicate info elements. If
I omit the statement <xsl:apply-templates select="@*|node()"/> from the
root template, the result tree is empty.

Can anyone help me see what is wrong with this stylesheet?

Thanks,

Ann Marie

I'm trying to transform this segment of xml:

<overview>
	<info>
	<para>overview paragraph A</para>
	</info>
	<info>
	<para>overview paragraph B</para>
	</info>
</overview>

To look like this:

<overview>
	<info>
	<para>overview paragraph A</para>
	<para>overview paragraph B</para>
	</info>
</overview>


<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output method="xml"/>
    <xsl:key name="Key" match="info" use="local-name()"/>

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

	<xsl:template match="overview">
		<xsl:copy>
			<info>
			<xsl:apply-templates select="*"/>
			</info>
		</xsl:copy>
		</xsl:template>

<xsl:template match="info">
	<xsl:apply-templates select="*"/>
</xsl:template>

<xsl:template match="*">
	<xsl:variable name="current" select="."/>
	<xsl:if test="not(../following-sibling::info[1]/*[. =
$current])">
		<xsl:copy-of select="."/>
	</xsl:if>
</xsl:template>

      <!-- Default Copy Statement. -->
    <xsl:template match="@*|node()" priority="1">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

 </xsl:stylesheet>

Here is the xml:

<?xml version="1.0"?>
	<doctitle>
		<prodtitle>prodtitle</prodtitle>
		<title>title</title>
	</doctitle>
	<subtitle>subtitle</subtitle>
	<copyright id="_1">
		<para>paragraph</para>
	</copyright>
	<preface>
		<purpose>
			<info>
				<para>paragraph</para>
			</info>
		</purpose>
		<audience>
			<info>
				<para>paragraph</para>
			</info>
		</audience>
		<organization>
			<info>
				<para>paragraph</para>
			</info>
		</organization>
		<relateddocumentation>
			<info>
				<para>paragraph</para>
			</info>
		</relateddocumentation>
		<boilerplate id="_2">
			<concept id="_3">
				<title>concept</title>
				<overview>
					<info>
						<para>overview
paragraph</para>
					</info>
				</overview>
					<subconcept>
					<title>subconcept</title>
				<overview>
						<info>
							<para>overview
paragraph A</para>
						</info>
						<info>
							<para>overview
paragraph B</para>
						</info>
					</overview>
					<info>
						<para>test</para>
					</info>
				</subconcept>
				<minitask>
					<title>minitask title</title>
					<overview>
						<info>
							<para>minitask
overview paragraph</para>
						</info>
					</overview>
					<minitasksteps>
						<minitaskstep>

<action>action</action>
						</minitaskstep>
					</minitasksteps>
				</minitask>
			</concept>
		</boilerplate>
	</preface>

	<section solution="all" id="_7" htmlfilename="test">
		<title>test</title>
	</section>
</document>

Current Thread