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

Re: Convert xml to another xml structure.

Subject: Re: Convert xml to another xml structure.
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Thu, 17 Nov 2005 07:28:46 +0100
convert xml to another xml
Hi Raj,

> Is it possible to arrange this XML to the following
> XML element using XSL and using level attributes.

Short answer: Yes.

I suppose that the real XML is wellformed....

<xsl:template match="/">
  <!-- This solution assumes that *[@level='1'] is the root of the document
-->
  <xsl:apply-templates select="*[@level='1']">
</xsl:template>

<xsl:template match="*">
  <xsl:param name="current-level" select="'1'"/>
  <xsl:variable name="current-id" select="generate-id()"/>
  <xsl:copy>  <!-- Shallow copy of the current node -->
  <xsl:copy-of select="@*"/>  <!-- Copy all the attributes -->
  <!-- Get the logical children (will be called recursively): -->
  <xsl:apply-templates
select="following-sibling::*[@level=$current-level+1][generate-id(preceding-s
ibling::*[@level=$current-level])=$current-id]"><xsl:with-param
name="current-level" select="$current-level+1"/></xsl:apply-templates>
  </xsl:copy>
</xsl:template>

If you change the elements name so that they are the same (say X),
then you can use X instead of * in this solution. This will make the
templates more specific.
This solution, I think, does not have anything that has not been
discussed with you before in various posts.

First predicate ([...]) ensures that the element in question will be
at a level deeper that the current one.
Second predicate ([...]) ensures that the template will not call
children of a sibling parent.

Others on this list have suggested that you try out some of the
tutorials that exists on the net. If you can get hold of "XSLT,
programmers reference (2nd ed)" by Michael Kay, I think there is an
example of this problem in that book. It is a great book for general
understanding XSL as well as the reference that its title says it is.

Regards,
Ragulf Pickaxe :-)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

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.
Email
First Name
Last Name
Company
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.