Subject: Re: [xsl] Creating Hierarchy From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 12 Oct 2008 14:45:42 -0400 |
I have an XML file that represents a tree of data elements, similar to a directory tree. The structure of the file has every node in the tree represented by a <node> element, all children of the root, with the hierarchy defined by a <level> element within each node. So the <node> with <level> = 0 is the root of the tree, its children have <level> = 1, their children have <level> = 2 etc.
Here's an example: ... I want to transform this into a hierarchical file like this: ... How do I write a stylesheet to do this?
T:\ftemp>type rowan.xml <mytree> <node> <name>Root of my tree</name> <level>0</level> </node> <node> <name>Child of root</name> <level>1</level> </node> <node> <name>Another child of root</name> <level>1</level> </node> <node> <name>Grandchild of root</name> <level>2</level> </node> <node> <name>Yet another child of root</name> <level>1</level> </node> </mytree>
T:\ftemp>xslt2 rowan.xml rowan.xsl con <?xml version="1.0" encoding="UTF-8"?> <newnode> <name>Root of my tree</name> <newnode> <name>Child of root</name> </newnode> <newnode> <name>Another child of root</name> <newnode> <name>Grandchild of root</name> </newnode> </newnode> <newnode> <name>Yet another child of root</name> </newnode> </newnode> T:\ftemp>type rowan.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<!--first time around start at level 0 and use all nodes--> <xsl:template match="mytree" name="next-level"> <xsl:param name="level" select="0"/> <xsl:param name="nodes" select="node"/>
-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video sample lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg Video course overview: http://www.youtube.com/watch?v=VTiodiij6gE G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Creating Hierarchy, Michael Kay | Thread | Re: [xsl] Creating Hierarchy, Rowan Sylvester-Brad |
RE: [xsl] Creating Hierarchy, Michael Kay | Date | [xsl] WordML to XML Header Problem, Byomokesh Sahoo |
Month |