xsl-list
[Top] [All Lists]

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

2007-04-19 20:58:12
Dimitre,

I believe I do know what the problem is and I'm sorry I've been unable to
communicate it clearly enough in my previous posts.

Let me try again.

I have a known tree structure.  Let's say it's stored in a file tree.xml.
It looks like this:

<a>
   <b>
     <c/>
     <d/>
   </b>
   <e>
     <f/>
     <g>
       <h/>
       <i/>
     </g>
   </e>
</a>

I have an application that generates boolean values (stored in an attribute
called "result") for a number of data elements.  The element names
correspond to the leaf node elements in the tree structure above.  Let's say
they are are stored in a file data.xml that looks like this:

<data>
  <c result="true"/>
  <d result="false"/>
  <h result="true"/>
  <i result="true"/>
</data>

Data.xml does not have to contain an element for each leaf node in the
tree.xml.

I am interested in constructing an output tree that has the same structure
as the tree above based only on the leaf node data.  To prevent ambiguity
there can only be one instance of each element in tree.xml.

The idea is that if all descendants of a node have a result attribute =
"true" then the result attribute value of that node is true also.  Ken
identified this in his opening paragraph even though I didn't make it clear.

In addition to the logical AND calculation, I also want to indicate where
leaf nodes are missing and that is where the "complete" attribute comes in.
So the output tree should indicate nodes that have missing descendants as
well as the logical AND result.  In the example I used, the desired output
tree is:

<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>

So in answer to your question, the element f can be absent from data.xml but
it needs to be in the output tree so it can identified as missing when the
output tree is transformed to HTML.  That is why all the leaf nodes have a
"complete" attribute.

Perhaps it is my choice of attributes that has caused confusion here.
Anyhow, I hope that this attempt to outline my requirements makes more sense
than the original post.

Regards, Simon

P.S.  Don't be deluded by my email address that I have much experience with
XML and XSLT.  I chose the domain name based on Oracle's database
terminology many years before I was even aware of XML.  I really am a
newbie.


-----Original Message-----
From: Dimitre Novatchev [mailto:dnovatchev(_at_)gmail(_dot_)com] 
Sent: April 19, 2007 5:11 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Constructing a tree from leaf nodes (knowing the tree
structure)?

This might have been an interesting problem, were it defined -- as of
this moment it is more of a puzzle what the OP actually wants...  ?
?

The "complete" attribute signifies whether all required descendent nodes
are
present or not.  For a leaf node I assume that if it is present
complete="true" and if it is absent complete="false".  Hence f has

If a leaf node is "absent", it cannot also have any attributes,
including a "complete" attribute. ? ? ?

Also, Dr. Kay's question why "f" should have its "complete" attribute
set to "false" remains unanswered.


It is quite often the case that if one cannot formulate well he is
asking help for, this person actually doesn't know what the problem
is.




On 4/19/07, Simon Shutter <simon(_at_)schemax(_dot_)com> wrote:
Thanks, Michael.

The "complete" attribute signifies whether all required descendent nodes
are
present or not.  For a leaf node I assume that if it is present
complete="true" and if it is absent complete="false".  Hence f has
complete="false".  I agree it is redundant for the data elements c,d,h and
i
to have complete="true" but I need the attribute to be present to that I
can
style the tree in HTML.

As far as the ancestors go, b is complete because all its descendants are
complete, while e and a are incomplete because some of their descendants
are
incomplete.

Simon


-----Original Message-----
From: Michael Kay [mailto:mike(_at_)saxonica(_dot_)com]
Sent: April 19, 2007 4:14 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Constructing a tree from leaf nodes (knowing the tree
structure)?

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(_at_)schemax(_dot_)com]
Sent: 19 April 2007 21:02
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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>



--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play

--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>
--~--