[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: AW: Merging common XML tree

Subject: Re: AW: Merging common XML tree
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Thu, 08 Jul 2010 11:02:33 +0100
Re: AW:  Merging common XML tree
You haven't specified the problem very completely. For example, are there always exactly two branches to be merged, or can it be any number? Can you get duplicates only at the top level, or at any level? What should happen if the dir elements have any attributes other than @name, etc?

I would do it in XSLT 2.0 using a recursive application of xsl:for-each-group. I wouldn't attempt it in XSLT 1.0 (but then, I wouldn't attempt anything in XSLT 1.0 unless my life depended on it).

<xsl:template name="g">
<xsl:param name="in" as="element(dir)*"/>
<xsl:for-each-group select="$in" group-by="name()">
<dir name="{@name}>
<xsl:call-template name="g">
<xsl:with-param name="in" select="current-group()/dir"/>

Not tested.

Michael Kay

On 08/07/2010 10:39, Szabo, Patrick (LNG-VIE) wrote:

You could try this:

<xsl:template match="dir">
		<copy-of select="@*">
	<xsl:if test="following::dir[@name = current()/@name]">
		<xsl:apply-templates select="following::dir[@name = current()/@name]/*"/>

Haven't tested this but the solution should be something like this.


Patrick Szabo XSLT-Entwickler

Tel.: +43 (1) 534 52 - 1573
Fax: +43 (1) 534 52 - 146


LexisNexis Verlag ARD Orac GmbH&  Co KG
Marxergasse 25, 1030 Wien
FN 8333f, Handelsgericht Wien

-----UrsprC<ngliche Nachricht-----

Von: Mathieu Malaterre [mailto:mathieu.malaterre@xxxxxxxxx]
Gesendet: Donnerstag, 08. Juli 2010 11:30
An: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Betreff:  Merging common XML tree

Hi there,

   I am trying to find a solution for the following problem. Let's
assume that my input XML tree looks like:

   <dir name="A">
     <dir name="B">
       <file name="C">
   <dir name="A">
     <dir name="B">
       <file name="D">

and my target output tree should looks like:

   <dir name="A">
     <dir name="B">
       <file name="C">
       <file name="D">

Basically I am trying to represent a dir/file structure in XML, where
I am not allowed to repeat common subdirectory. What XSL function
could I use to do that (XSTL 1.0 if possible).

Thanks !

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.