Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Building a tree from path-like strings

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 1/3/2009 7:12:00 PM
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent