xsl-list
[Top] [All Lists]

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

2006-07-27 09:46:32
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(_dot_)oldham(_at_)kilo75(_dot_)com> 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:

+----+---------------+------+
| id | name          | path |
+----+---------------+------+
|  1 | About         | 1    |
|  2 | Services      | 2    |
|  3 | Environmental | 2.1  |
|  4 | Landscaping   | 2.2  |
+----+---------------+------+

And this were converted to XML, eg:

<directories>
 <item>
   <id>1</id>
   <name>About</name>
   <path>1</path>
 </item>
 <item>
   <id>2</id>
   <name>Services</name>
   <path>2</path>
 </item>
 ... etc ...
</directories>

Would it be relatively easy to use the path to build a tree stucture? eg:

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

Are there any disadvantages to using Materialized Paths?

Phill

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>