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

Re: make recursive call

Subject: Re: make recursive call
From: "mary liu" <maryliu99@xxxxxxxxx>
Date: Thu, 7 Sep 2006 16:56:18 -0400
mary liu
Thanks Mukul! It is very helpful.

Another question is if I have exception data, like the last 2 records.
I want to genterate like newxml.xml.

oldxml.xml


<ResultSet> <data_service> <Employeeprofile> <row emp_name="one" org_cd="73700" rpt_to_org_cd="11111" lev="2" /> <row emp_name="four" org_cd="73656" rpt_to_org_cd="73700" lev="3" /> <row emp_name="five" org_cd="75425" rpt_to_org_cd="73700" lev="3" /> <row emp_name="three" org_cd="73723" rpt_to_org_cd="11111" lev="2" /> <row emp_name="two" org_cd="73708" rpt_to_org_cd="11111" lev="2" /> <row emp_name="Six" org_cd="73651" rpt_to_org_cd="73708" lev="3" /> <row emp_name="Seven" org_cd="73632" rpt_to_org_cd="73651" lev="4" /> <row emp_name="Eight" org_cd="73229" rpt_to_org_cd="73651" lev="4" /> <row emp_name="nine" org_cd="74023" rpt_to_org_cd="73651" lev="4" /> <row emp_name="ten" org_cd="73989" rpt_to_org_cd="11111" lev="2" />

<row emp_name="eleven" org_cd="897750" rpt_to_org_cd="UHFH"  lev="4" />
<row emp_name="twelve" org_cd="3334" rpt_to_org_cd="4456"  lev="2" />
</Employeeprofile>
</data_service>
</ResultSet>


the newxml.xml


So the newxml.xml should be like this:


<ResultSet> <branch id="one"> <branchText>one</branchText> <leaf> <leafText>four</leafText> </leaf> <leaf> <leafText>five</leafText> </leaf> </branch>


<branch id="three"> <branchText>three</branchText> </branch>


<branch id="two"> <branchText>two</branchText> <branch id="Six"> <branchText>Six</branchText> <leaf> <leafText>Seven</leafText> </leaf> <leaf> <leafText>Eight</leafText> </leaf> <leaf> <leafText>nine</leafText> </leaf> </branch> </branch>


<branch id="ten"> <branchText>ten</branchText> </branch>

  <branch id="eleven">
    <branchText>eleven</branchText>
 </branch>

<branch id="twelve">
    <branchText>twelve</branchText>
 </branch>
</ResultSet>



Re:  make recursive call
Subject: Re:  make recursive call
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 26 Aug 2006 22:46:12 +0530

Please try this stylesheet:


<?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" indent="yes" />


<xsl:template match="/"> <ResultSet> <xsl:apply-templates select="ResultSet/data_service/Employeeprofile/row[@lev = 2]" /> </ResultSet> </xsl:template>


<xsl:template match="row"> <branch id="{@emp_name}"> <branchText> <xsl:value-of select="@emp_name"/> </branchText> <xsl:variable name="row1" select="../row[@rpt_to_org_cd = current()/@org_cd]" /> <xsl:variable name="row2" select="../row[@rpt_to_org_cd = $row1/@org_cd]" /> <xsl:choose> <xsl:when test="$row2"> <xsl:apply-templates select="$row1" /> </xsl:when> <xsl:otherwise> <xsl:for-each select="$row1"> <leaf> <leafText><xsl:value-of select="@emp_name" /></leafText> </leaf> </xsl:for-each> </xsl:otherwise> </xsl:choose> </branch> </xsl:template>

</xsl:stylesheet>

This when applied to the given XML, produces the desired output.

On 8/25/06, mary liu <maryliu99@xxxxxxxxx> wrote:
Hi,


I have a oldxml.xml file need to be converted to the newxml.xml file. I can only convert it to 2 levels and dont know how to make it recursive. I don't know how deepth it is. My convert file is like this:

conver.xsl


<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform";> <xsl:template match="/">


<ResultSet> <xsl:apply-templates select="ResultSet/data_service/Employeeprofile/row[@lev = 2]"/> </ResultSet>


</xsl:template> <xsl:template match="row"> <xsl:element name="branch"> <xsl:attribute name="id"> <xsl:value-of select="@emp_name" /> </xsl:attribute>


<branchText> <xsl:value-of select="@emp_name"/> </branchText> <xsl:call-template name="IterateRows"> <xsl:with-param name="rows" select="following-sibling::node()"/> </xsl:call-template> </xsl:element> </xsl:template> <xsl:template name="IterateRows"> <xsl:param name="rows"/> <xsl:if test="$rows[1]/@lev = 3"> <leaf> <leafText> <xsl:value-of select="$rows[1]/@emp_name"/> </leafText>


</leaf> <xsl:call-template name="IterateRows"> <xsl:with-param name="rows" select="$rows[position()> 1]"/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet>




oldxml.xml



<ResultSet> <data_service> <Employeeprofile> <row emp_name="one" org_cd="73700" rpt_to_org_cd="11111" lev="2" /> <row emp_name="four" org_cd="73656" rpt_to_org_cd="73700" lev="3" /> <row emp_name="five" org_cd="75425" rpt_to_org_cd="73700" lev="3" /> <row emp_name="three" org_cd="73723" rpt_to_org_cd="11111" lev="2" /> <row emp_name="two" org_cd="73708" rpt_to_org_cd="11111" lev="2" /> <row emp_name="Six" org_cd="73651" rpt_to_org_cd="73708" lev="3" /> <row emp_name="Seven" org_cd="73632" rpt_to_org_cd="73651" lev="4" /> <row emp_name="Eight" org_cd="73229" rpt_to_org_cd="73651" lev="4" /> <row emp_name="nine" org_cd="74023" rpt_to_org_cd="73651" lev="4" /> <row emp_name="ten" org_cd="73989" rpt_to_org_cd="11111" lev="2" /> </Employeeprofile> </data_service> </ResultSet>


lev="2" is same level. Then lev=3, lev=4... every level may have leaves or may not have leaves, like emp_name="two", it has no leaf.


Attribute rpt_to_org_cd is the value pointing to the parent. for example emp_name="Eight", rpt_to_org_cd ="73651", its parents' node is same as emp_name="Seven" and emp_name="nine" which is emp_name="Six" org_cd="73651"

So the newxml.xml should be like this:


<ResultSet> <branch id="one"> <branchText>one</branchText> <leaf> <leafText>four</leafText> </leaf> <leaf> <leafText>five</leafText> </leaf> </branch>


<branch id="three"> <branchText>three</branchText> </branch>


<branch id="two"> <branchText>two</branchText> <branch id="Six"> <branchText>Six</branchText> <leaf> <leafText>Seven</leafText> </leaf> <leaf> <leafText>Eight</leafText> </leaf> <leaf> <leafText>nine</leafText> </leaf> </branch> </branch>


<branch id="ten"> <branchText>ten</branchText> </branch>

</ResultSet>

Is there anybody helping me? Thanks in advance.

Mary

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.