Re: [xsl] merging duplicate consecutive elements

Subject: Re: [xsl] merging duplicate consecutive elements
From: omprakash.v@xxxxxxxxxxxxx
Date: Wed, 19 Oct 2005 09:38:28 +0530
Hi,

You don't need a priority when you use the identity transform.

So your
      <!-- Default Copy Statement. -->
    <xsl:template match="@*|node()" priority="1">
can just be

      <!-- Default Copy Statement. -->
    <xsl:template match="@*|node()">

I couldn't verify if the entire output is correct. But the grouping of
adjacent info nodes happens correctly.

I modified your stylesheet slightly which can be seen below:

Here's the stylesheet:

<?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="*"
mode="group"/>
                                     </info>
                         </xsl:copy>
                         </xsl:template>


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

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

<xsl:template match="info/*" mode="group">
             <xsl:variable name="current" select="."/>

 <xsl:if test="../following-sibling::info[1]">
            <xsl:copy-of select="."/>
 </xsl:if>

            <xsl:copy-of select="../following-sibling::info[1]/para"/>


</xsl:template>

      <!-- Default Copy Statement. -->
    <xsl:template match="@*|node()">

        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

 </xsl:stylesheet>

cheers,
prakash







                                                                                                                                
                      "Annmarie Rubin                                                                                           
                      \(anrubin\)"             To:      <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>                                       
                      <anrubin@xxxxxxx         cc:      "Annmarie Rubin \(anrubin\)" <anrubin@xxxxxxxxx>, (bcc:                 
                      om>                      omprakash.v/Polaris)                                                             
                                               Subject: [xsl] merging duplicate consecutive elements                            
                      10/19/2005 06:55                                                                                          
                      AM                                                                                                        
                      Please respond                                                                                            
                      to xsl-list                                                                                               
                                                                                                                                
                                                                                                                                




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>





This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. 
If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately.
You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification,
distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit Us at http://www.polaris.co.in

Current Thread