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

RE: [xsl] Constructing a tree from leaf nodes (knowing the tree structure)?


Subject: RE: [xsl] Constructing a tree from leaf nodes (knowing the tree structure)?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 20 Apr 2007 00:13:41 +0100

XSLT is certainly suited to the problem. However, I can't reverse engineer
your requirements from your example. The following stylesheet comes close,
and there are various ways I could refine it to produce your required
output, but I'd be using guesswork as to what the requirements are, so it
would be better if you do that yourself! In particular I can't see why you
consider b to be complete while f is incomplete, and since all the data
elements say complete="true", it's hard to see what role that attribute
plays in the calculation.

I used your tree as the principal input, and wrapped the other nodes in a
<data> element and called it data.xml. The stylesheet is:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:variable name="tree" select="/"/>
<xsl:variable name="data" select="doc('data.xml')/data"/>

<xsl:template match="*">
  <xsl:copy>
    <xsl:attribute name="complete" 
       select="every $n in descendant::* satisfies 
                 exists($data/*[name() = name($n)])"/>
    <xsl:attribute name="result"
           select="every $n in descendant::* satisfies 
                 exists($data/*[name() = name($n) and @result='true']) "/>
    <xsl:apply-templates/>             
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>         

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Simon Shutter [mailto:simon@xxxxxxxxxxx] 
> Sent: 19 April 2007 21:02
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Constructing a tree from leaf nodes (knowing 
> the tree structure)?
> 
> 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.
> 
> <a>
>   <b>
>     <c/>
>     <d/>
>   </b>
>   <e>
>     <f/>
>     <g>
>       <h/>
> 	<i/>
>     </g>
>   </e>
> </a>
> 
> I then have data for some of the leaf nodes ie.
> 
> <c complete="true" result="true"/>
> <d complete="true" result="false"/>
> <h complete="true" result="true"/>
> <i complete="true" result="true"/>
> 
> 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>


Current Thread
Keywords