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

Re: Building a tree from path-like strings

Subject: Re: Building a tree from path-like strings
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 03 Jan 2009 14:11:15 -0500
Re:  Building a tree from path-like strings
At 2009-01-03 18:38 +0000, Richard Lewis wrote:
Hi there,

I'm trying to make a tree-hierarchic document from a flat
list of elements which have id-like values which define
the hierarchy I need.

So here is simplified version of the source document:
...
which should become this after transformation:
...
  <li id="FOO/2">
    FOO/3
  </li>

I'm assuming you really want id="FOO/3" there.


So far I have the following:
...
This attempt at a solution is not only protracted and incomprehensible, it
doesn't actually work either.

I chose to start from scratch.


Can anyone think of a solution to this? Preferably disregarding my
existing attempt.

A complete solution to your supplied data is below whose output matches what you specified, modulo some indentation controlled by the XSLT processor.


Note my use of recursion, rather than re-application. I felt that dealing with axes when applying templates would be more cumbersome than dealing with progressively smaller subsets of records. Each subset is an ordered list. The result is quite compact, and when dealing with recursion I often find the more compact the result the more robust it usually is ... once you start dealing with exceptions, problems can creep in unexpectedly.

I hope this helps.

. . . . . . . . . . . . Ken

T:\ftemp>type richard.xml
<btc>
<record table="works">
  <record table="external_records">
    <field name="external_id"><value>FOO/1</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/1/1</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/1/2</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/2</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/2/1</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/2/1/1</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/2/1/2</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/2/2</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
  <record table="external_records">
    <field name="external_id"><value>FOO/3</value></field>
    <field name="ds_name"><value>ms-sources</value></field>
  </record>
</record>
</btc>

T:\ftemp>call xslt richard.xml richard.xsl richard.out

T:\ftemp>type richard.out
<?xml version="1.0" encoding="utf-8"?>
<ol>
   <li id="FOO/1">FOO/1<ol>
         <li id="FOO/1/1">FOO/1/1</li>
         <li id="FOO/1/2">FOO/1/2</li>
      </ol>
   </li>
   <li id="FOO/2">FOO/2<ol>
         <li id="FOO/2/1">FOO/2/1<ol>
               <li id="FOO/2/1/1">FOO/2/1/1</li>
               <li id="FOO/2/1/2">FOO/2/1/2</li>
            </ol>
         </li>
         <li id="FOO/2/2">FOO/2/2</li>
      </ol>
   </li>
   <li id="FOO/3">FOO/3</li>
</ol>
T:\ftemp>type richard.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <xsl:call-template name="recurse-this">
    <xsl:with-param name="records" select="btc/record/record"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="recurse-this">
<xsl:param name="records"/>
<ol>
<!--work only with the records at this level of nesting-->
<xsl:for-each select="$records">
<xsl:variable name="this-id" select="field[@name='external_id']/value"/>
<xsl:if test="count($records[starts-with($this-id,
field[@name='external_id']/value)]) = 1">
<!--then this a record at the current level in the list-->
<li id="{$this-id}">
<xsl:value-of select="$this-id"/>
<!--are there other deeper records?-->
<xsl:if test="count($records[
starts-with(field[@name='external_id']/value,$this-id)]) > 1">
<!--then this needs to have a nested list-->
<xsl:call-template name="recurse-this">
<!--but only use those items that are "deeper" than this-->
<xsl:with-param name="records"
select="$records[
starts-with(field[@name='external_id']/value,$this-id) and
field[@name='external_id']/value!=$this-id]"/>
</xsl:call-template>
</xsl:if>
</li>
</xsl:if>
</xsl:for-each>
</ol>
</xsl:template>


</xsl:stylesheet>

T:\ftemp>rem Done!


-- Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes: : Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 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

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-2007 All Rights Reserved.