Re: [xsl] merging duplicate consecutive elements

Subject: Re: [xsl] merging duplicate consecutive elements
From: Martin Gadbois <martin.gadbois@xxxxxxxxxxxx>
Date: Wed, 19 Oct 2005 09:05:24 -0400
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This stylesheet does the trick:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="overview">
        <xsl:copy>
            <xsl:element name="info">
                <xsl:apply-templates select="descendant::para"/>
            </xsl:element>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

It is basically the identity transform, plus the exception for <overview/>.


Annmarie Rubin (anrubin) wrote:
> 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>
> 
> 


- --
==============         +----------------------------------------------+
Martin Gadbois         | "Windows might take you from 0 to 60 faster, |
S/W Developer          |  but to go to 100 you need Unix."            |
Colubris Networks Inc. +----------------------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDVkSU9Y3/iTTCEDkRAsTpAJ4lSwieack5y864pdMH3I+INPGJjQCfWZ8W
uj2YpLIXx9sDjVYpbPNSWcw=
=TPEB
-----END PGP SIGNATURE-----

Current Thread