|
Subject: Re: [xsl] Constructing a tree from leaf nodes (knowing the tree structure)? From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Thu, 19 Apr 2007 16:43:41 -0400 |
This is probably a poorly posed question but essentially I am trying to determine if XSLT is suited to the following problem.
Say I have a node fragment that defines a tree structure in which each element appears only once eg. ... I then have data for some of the leaf nodes ie. ... In this example the leaf node </f> is missing.
Is it possible to create a node fragment that mimics the tree structure and sets ancestor attributes according to the presence or absence of leaf nodes and their attributes?
The desired output would be:
<a complete="false" result="false"> <b complete="true" result="false"> <c complete="true" result="true"/> <d complete="true" result="false"/> </b> <e complete="false" result="true"> <f complete="false" result=""/> <g complete="true" result="true"/> <h complete="true" result="true"/> <i complete="true" result="true"/> </g> </e> </a>
T:\ftemp>type shutter.out
<a complete="false" result="false">
<b complete="true" result="false">
<c complete="true" result="true"/>
<d complete="true" result="false"/>
</b>
<e complete="false" result="true">
<f complete="false" result=""/>
<g complete="true" result="true">
<h complete="true" result="true"/>
<i complete="true" result="true"/>
</g>
</e>
</a>
T:\ftemp>type leaves.xml
<leaves>
<c complete="true" result="true"/>
<d complete="true" result="false"/>
<h complete="true" result="true"/>
<i complete="true" result="true"/>
</leaves>T:\ftemp>type shutter.xml
<a>
<b>
<c/>
<d/>
</b>
<e>
<f/>
<g>
<h/>
<i/>
</g>
</e>
</a>T:\ftemp>type shutter.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="*">
<xsl:copy>
<xsl:attribute name="complete">
<xsl:choose>
<!--at a leaf use the value from leaves, or 'false' if not there-->
<xsl:when test="not(*)">
<xsl:value-of select="( key('leaves',name(.),$leaves)/@complete,
'false' )[1]"/>
</xsl:when>
<!--at a branch, check all leaves for any not being in leaf file-->
<xsl:when test=".//*[not(*)][not(key('leaves',name(.),$leaves))]"
>false</xsl:when>
<!--there are no absent leaves, so this is complete-->
<xsl:otherwise>true</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="result">
<xsl:choose>
<!--at a leaf, use the value from leaves, or '' if not there-->
<xsl:when test="not(*)">
<xsl:value-of select="key('leaves',name(.),$leaves)/@result"/>
</xsl:when>
<!--at a branch, check all leaves for any being false-->
<xsl:when test=".//*[not(*)]/key('leaves',name(.),$leaves)/@result =
'false'">false</xsl:when>
<!--there are none that are false, so then all must be true-->
<xsl:otherwise>true</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template></xsl:stylesheet> T:\ftemp>rem Done!
-- World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| [xsl] Constructing a tree from leaf, Simon Shutter | Thread | RE: [xsl] Constructing a tree from , Simon Shutter |
| Re: [xsl] XSLT recursive template t, David Carlisle | Date | RE: [xsl] Constructing a tree from , Simon Shutter |
| Month |