Subject: recursive XSLT Help
Posted By: boates Post Date: 1/11/2006 1:23:18 PM
Hi I am trying to do the following with a xslt page for xml output.  I am not doing html rendering and I need all the elements to output

1) Have a flat DB Table that I read into XML.  It comes out as
<NODES>
  <NODE>
     <name>A</name>
     <id>1</id>
     <parent>Root</parent>
  </NODE>
  <NODE>
     <name>B</name>
     <id>2</id>
     <parent>1</parent>
  </NODE>
  <NODE>
     <name>C</name>
     <id>3</id>
     <parent>2</parent>
  </NODE>
  <NODE>
     <name>D</name>
     <id>4</id>
     <parent>Root</parent>
  </NODE>
  <NODE>
     <name>E</name>
     <id>5</id>
     <parent>4</parent>
  </NODE>
  <NODE>
     <name>F</name>
     <id>6</id>
     <parent>4</parent>
  </NODE>
</NODES>

2) need to change the format to this so I can use it in a web control
This is a nested xml file that shows the relationship
<NODES>
  <NODE>
     <name>A</name>
     <id>1</id>
     <parent>Root</parent>
     <NODE>
        <name>B</name>
        <id>2</id>
        <parent>1</parent>
        <NODE>
           <name>C</name>
           <id>2</id>
           <parent>Root</parent>
        </NODE>
     </NODE>
  </NODE>
  <NODE>
     <name>D</name>
     <id>4</id>
     <parent>Root</parent>
     <NODE>
        <name>E</name>
        <id>5</id>
        <parent>4</parent>
     </NODE>
     <NODE>
        <name>F</name>
        <id>6</id>
        <parent>4</parent>
     </NODE>
  </NODE>
</NODES>

Thanks,
Ben
Reply By: mhkay Reply Date: 1/11/2006 2:08:40 PM
You want something like this:

<xsl:template match="/">
  <xsl:apply-templates select="/NODES/NODE[parent='Root']"/>
</xsl:template>

<xsl:template match="NODE">
  <NODE>
    <xsl:copy-of select="*"/>
    <xsl:apply-templates select="/NODES/NODE[parent=current()/id]"/>
  </NODE>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply By: boates Reply Date: 1/11/2006 2:50:00 PM
Michael,

I modified (to what I need) and tested out your transform in altovaXML and CookTop xslt parsers.  This is exactly the solution I needed.  I will check out your book!! Hopefully you have a recursive chapter.


Cheers,
Ben


Go to topic 38592

Return to index page 397
Return to index page 396
Return to index page 395
Return to index page 394
Return to index page 393
Return to index page 392
Return to index page 391
Return to index page 390
Return to index page 389
Return to index page 388