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

RE: Flattening a XML tree

Subject: RE: Flattening a XML tree
From: "Casadome, Francisco Javier" <Francisco.Casadome@xxxxxxxxxxxxxx>
Date: Tue, 2 Oct 2001 08:30:03 +0200
treenode id
Hi Farroq,

Try this XSL:

<?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" omit-xml-declaration="no" indent="yes"/>

<xsl:template match="/Tree">
  <Tree>
    <xsl:for-each select="//TreeNode">
    <xsl:sort select="@id" data-type="number"/>
	<xsl:element name="TreeNode">
	  <xsl:attribute name="id">
          <xsl:value-of select="@id"/>
        </xsl:attribute>
	  <xsl:attribute name="weight">
          <xsl:value-of select="@weight"/>
        </xsl:attribute>
	</xsl:element>
    </xsl:for-each>
  </Tree>
</xsl:template>

</xsl:stylesheet>

It will produce the following XML (don't know if this was exactly what you
wanted):

<?xml version="1.0" encoding="UTF-16"?>
<Tree>
	<TreeNode id="1" weight="5"/>
	<TreeNode id="2" weight="3"/>
	<TreeNode id="3" weight="4"/>
	<TreeNode id="4" weight="2"/>
	<TreeNode id="5" weight="2"/>
	<TreeNode id="6" weight="2"/>
	<TreeNode id="7" weight="2"/>
	<TreeNode id="8" weight="2"/>
	<TreeNode id="9" weight="3"/>
	<TreeNode id="10" weight="2"/>
	<TreeNode id="11" weight="2"/>
	<TreeNode id="12" weight="2"/>
</Tree>

Cheers,
Frank.

-----Original Message-----
From: Mir Farooq Ali [mailto:mfali@xxxxxxxxxxxxxxxx] 
Sent: martes, 02 de octubre de 2001 6:00
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject:  Flattening a XML tree


Hi,

I have an input tree that looks like this

<?xml version="1.0"?>
<Tree>
 <TreeNode id="1" weight="5">
  <TreeNode id="2" weight="3">
   <TreeNode id="6" weight="2"/>
   <TreeNode id="7" weight="2"/>
   <TreeNode id="8" weight="2"/>
  </TreeNode>
  <TreeNode id="3" weight="4">
   <TreeNode id="9" weight="3">
    <TreeNode id="11" weight="2"/>
    <TreeNode id="12" weight="2"/>
   </TreeNode>
   <TreeNode id="10" weight="2"/>
  </TreeNode>
  <TreeNode id="4" weight="2"/>
  <TreeNode id="5" weight="2"/>
 </TreeNode>
</Tree>

I need to flatten the tree to a structure that looks like this. I've
looked at the archives, but haven't found an answer.

<?xml version="1.0"?>
<Tree>
  <TreeNode id="1" weight="5">
    <TreeNode id="2" weight="3"/>
    <TreeNode id="3" weight="4"/>
    <TreeNode id="4" weight="2"/>
    <TreeNode id="5" weight="2"/>
    <TreeNode id="6" weight="2"/>
    <TreeNode id="7" weight="2"/>
    <TreeNode id="8" weight="2"/>
    <TreeNode id="11" weight="2"/>
    <TreeNode id="12" weight="2"/>
    <TreeNode id="9" weight="3"/>
    <TreeNode id="10" weight="2"/>
  </TreeNode>
</Tree>

Any help is appreciated.

Thanks,

-Farooq.

+---------------------------------+-------------------------------------+
| Ph.D. Candidate                 | (540) 5520967 (R)  (540) 2313986 (W)|
| Department of Computer Science  | Email:                mfali@xxxxxx  |
| Virginia Tech.                  | Web: http://csgrad.cs.vt.edu/~mfali |
+---------------------------------+-------------------------------------+



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


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.