Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Grouping and removing duplicates from xml: need help

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 8/12/2007 3:26:00 PM

atomnyjwzryw@t... wrote:
> On Aug 10, 7:09 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
>> atomnyjwz...@tlen.pl wrote:
>>> how can I group by filename and remove duplicate nodes with attribute
>>> name="nazwa" using XSLT? I want transform one xml to another but I
>>> don't have any idea. Here is an example and expected result.

The following stylesheet should do what you want:

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

   <xsl:output method="xml" indent="yes"/>

   <xsl:strip-space elements="*"/>

   <xsl:key name="file-group"
            match="zad"
            use="record/prop[@name='filename']"/>

   <xsl:key name="nazwa-group"
            match="sub"
            use="concat(../../prop[@name='filename'], '_', 
prop[@name='nazwa'])"/>

   <xsl:template match="root">
     <xsl:copy>
       <xsl:apply-templates select="zad[generate-id() = 
generate-id(key('file-group', record/prop[@name='filename'])[1])]"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="zad">
     <Obj id="File">
       <Fd name="FileName"><xsl:value-of 
select="record/prop[@name='filename']"/></Fd>
     </Obj>
     <xsl:apply-templates select="key('file-group', 
record/prop[@name='filename'])/record/zbior/sub[generate-id() = 
generate-id(key('nazwa-group', concat(../../prop[@name='filename'], '_', 
prop[@name='nazwa']))[1])]"/>
   </xsl:template>

   <xsl:template match="sub">
     <Obj id="col">
       <xsl:copy-of select="prop"/>
     </Obj>
   </xsl:template>

</xsl:stylesheet>

When run on this input

<root>
	<zad>
	  <record>
	    <prop name="filename">c:\test1</prop>
	    <prop backup="nrbackup">23</prop>
	    <zbior>
	      <sub>
		<prop name="nazwa">a</prop>
		<prop name="level">4</prop>
		<prop name="dom">df</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">b</prop>
		<prop name="level">45</prop>
		<prop name="dom">sd</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">a</prop>
		<prop name="level">4</prop>
		<prop name="dom">df</prop>
	      </sub>
	    </zbior>
	  </record>
	</zad>

	<zad>
	  <record>
	    <prop name="filename">c:\test1</prop>
	    <prop backup="nrbackup">23</prop>
	    <zbior>
	      <sub>
		<prop name="nazwa">a</prop>
		<prop name="level">4</prop>
		<prop name="dom">df</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">c</prop>
		<prop name="level">55</prop>
		<prop name="dom">rt</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">b</prop>
		<prop name="level">45</prop>
		<prop name="dom">sd</prop>
	      </sub>
	    </zbior>
	  </record>
	</zad>

	<zad>
	  <record>
	    <prop name="filename">c:\test2</prop>
	    <prop backup="nrbackup">23</prop>
	    <zbior>
	      <sub>
		<prop name="nazwa">aa</prop>
		<prop name="level">14</prop>
		<prop name="dom">dfa</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">cc</prop>
		<prop name="level">255</prop>
		<prop name="dom">srt</prop>
	      </sub>
	      <sub>
		<prop name="nazwa">aa</prop>
		<prop name="level">14</prop>
		<prop name="dom">dfa</prop>
	      </sub>
	    </zbior>
	  </record>
	</zad>

</root>

it creates the following result:

<root>
    <Obj id="File">
       <Fd name="FileName">c:\test1</Fd>
    </Obj>
    <Obj id="col">
       <prop name="nazwa">a</prop>
       <prop name="level">4</prop>
       <prop name="dom">df</prop>
    </Obj>
    <Obj id="col">
       <prop name="nazwa">b</prop>
       <prop name="level">45</prop>
       <prop name="dom">sd</prop>
    </Obj>
    <Obj id="col">
       <prop name="nazwa">c</prop>
       <prop name="level">55</prop>
       <prop name="dom">rt</prop>
    </Obj>
    <Obj id="File">
       <Fd name="FileName">c:\test2</Fd>
    </Obj>
    <Obj id="col">
       <prop name="nazwa">aa</prop>
       <prop name="level">14</prop>
       <prop name="dom">dfa</prop>
    </Obj>
    <Obj id="col">
       <prop name="nazwa">cc</prop>
       <prop name="level">255</prop>
       <prop name="dom">srt</prop>
    </Obj>
</root>

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent