[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XSL, SQL, and trees (again)


Subject: Re: [xsl] XSL, SQL, and trees (again)
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Thu, 27 Jul 2006 22:16:07 +0530

Hi Phill,
 Here is a XSLT 1.0 solution to convert,

<directories>
<item>
  <id>1</id>
  <name>About</name>
  <path>1</path>
</item>
<item>
  <id>2</id>
  <name>Services</name>
  <path>2</path>
</item>
<item>
  <id>3</id>
  <name>Environmental</name>
  <path>2.1</path>
</item>
<item>
  <id>4</id>
  <name>Landscaping</name>
  <path>2.2</path>
</item>
</directories>

into,

<directories>
  <directory name="About"/>
  <directory name="Services">
     <directory name="Environmental"/>
     <directory name="Landscaping"/>
  </directory>
</directories>

<?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="/directories">
 <directories>
   <xsl:for-each select="item[(string-length(path) -
string-length(translate(path, '.', ''))) = 0]">
     <directory name="{name}">
       <xsl:call-template name="re-arrange">
         <xsl:with-param name="nodeset" select="../item" />
         <xsl:with-param name="curr-path" select="path" />
         <xsl:with-param name="no-of-dots" select="1" />
       </xsl:call-template>
     </directory>
   </xsl:for-each>
 </directories>
</xsl:template>

<xsl:template name="re-arrange">
 <xsl:param name="nodeset" />
 <xsl:param name="curr-path" />
 <xsl:param name="no-of-dots" />

 <xsl:for-each select="$nodeset[((string-length(path) -
string-length(translate(path, '.', ''))) = $no-of-dots) and
(starts-with(path, $curr-path))]">
   <directory name="{name}">
     <xsl:call-template name="re-arrange">
      <xsl:with-param name="nodeset" select="$nodeset" />
      <xsl:with-param name="curr-path" select="path" />
      <xsl:with-param name="no-of-dots" select="$no-of-dots + 1" />
     </xsl:call-template>
   </directory>
 </xsl:for-each>

</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

http://gandhimukul.tripod.com

On 7/27/06, Phillip B Oldham <phillip.oldham@xxxxxxxxxx> wrote:
Hi all

I've been looking at the different methods for working with storing
trees in SQL.

At the moment, I'm looking at the 'Materialized Path' technique, and
have a couple of questions regarding XSL.

If I were to have a table along the following lines:


Current Thread
Keywords