Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML: Collecting ancestors

Reply
Thread Tools

XML: Collecting ancestors

 
 
yurick
Guest
Posts: n/a
 
      01-14-2004
Hello everybody,

I'm quit newbie to XML/XSLT, would appreciate any help.

There is XML like this:

<person name="adam"/>
<person name="eve"/>
<person name="cain">
<parent name="adam"/>
<parent name="eve"/>
</person>
<person name="henoch">
<parent name="cain"/>
</person>

I need to get output from transformation:

adam(0)
eve(0)
cain(2): adam, eve
henoch(3): cain, adam, eve

i.e. name of the person, total number and list of all parents, grandparents,
grand-grandparents, grand-grand-grandparents, etc.

Is it possible to express this in XSLT with recursive of any deepness?
Or maybe other technique is applicable here?

-- Regards, Yurii
 
Reply With Quote
 
 
 
 
Ed Beroset
Guest
Posts: n/a
 
      01-15-2004
yurick wrote:

> <person name="henoch">
> <parent name="cain"/>
> </person>
>
> I need to get output from transformation:
>
> adam(0)
> eve(0)
> cain(2): adam, eve
> henoch(3): cain, adam, eve


First, since XML requires a single root tag, I added <people> around
your collection of names. Second, I didn't add commas in the output
stream, but that's just because I'm lazy. It's certainly possible to do.

> Is it possible to express this in XSLT with recursive of any deepness?
> Or maybe other technique is applicable here?


Here's the modified input file:

<?xml version="1.0" encoding="iso-8859-1"?>
<people>
<person name="adam"/>
<person name="eve"/>
<person name="cain">
<parent name="adam"/>
<parent name="eve"/>
</person>
<person name="henoch">
<parent name="cain"/>
</person>
<person name="gladys"/>
<person name="frank">
<parent name="henoch"/>
</person>
<person name="jose">
<parent name="gladys"/>
<parent name="frank"/>
</person>
</people>

And this is the XSLT I wrote:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnssd="http://www.w3.org/2001/XMLSchema"
>

<xslutput method="text"/>

<xsl:template match="/people">
<xsl:apply-templates select="person"/>
</xsl:template>

<!-- for each person, print the name and ancestor count -->
<xsl:template match="person">
<xsl:value-of select="@name"/>
<xsl:text>(</xsl:text>
<xsl:apply-templates select="." mode="count-kin"/>
<xsl:text>): </xsl:text>
<!-- now name all of the ancestors -->
<xsl:apply-templates select="." mode="name-kin"/>
<xsl:text>
</xsl:text>
</xsl:template>

<!-- recursively name the ancestors -->
<xsl:template match="parent" mode="name-kin">
<xsl:value-of select="concat(@name,' ')"/>
<xsl:variable name="myname" select="@name"/>
<xsl:apply-templates select="/people/person[@name=$myname]"
mode="name-kin"/>
</xsl:template>

<!-- recursively name the ancestors -->
<xsl:template match="person" mode="name-kin">
<xsl:apply-templates select="parent" mode="name-kin"/>
</xsl:template>


<!-- recursively count the number of ancestors -->
<xsl:template match="parent" mode="count-kin">
<xsl:variable name="myname" select="@name"/>
<xsl:variable name="ancestor-count">
<xsl:apply-templates select="/people/person[@name=$myname]"
mode="count-kin"/>
</xsl:variable>
<xsl:value-of select="$ancestor-count"/>
</xsl:template>

<!-- recursively count the number of ancestors -->
<xsl:template match="person" mode="count-kin">
<xsl:choose>
<!-- only count ancestors if there are any -->
<xsl:when test="count(parent)">
<xsl:variable name="ancestor-count">
<xsl:apply-templates select="parent" mode="count-kin"/>
</xsl:variable>
<xsl:value-of select="number($ancestor-count)+count(parent)"/>
</xsl:when>
<!-- no parents -->
<xsltherwise>
<xsl:value-of select="0"/>
</xsltherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

The results look like this:

adam(0):
eve(0):
cain(2): adam eve
henoch(3): cain adam eve
gladys(0):
frank(4): henoch cain adam eve
jose(6): gladys frank henoch cain adam eve

That should give you some ideas.

Ed

 
Reply With Quote
 
 
 
 
yurick
Guest
Posts: n/a
 
      01-15-2004
Ed Beroset <> wrote in message news:<fjlNb.8488$ nk.net>...
> That should give you some ideas.
>
> Ed


Thank You!!!!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Logging ancestors ignored if config changes? andrew cooke Python 2 04-26-2008 03:05 PM
Trickery in the ancestors chain Paolo Nusco Perrotta Ruby 9 01-25-2007 08:53 AM
Ancient Ancestors Old Gringo Computer Support 16 03-04-2005 09:23 PM
How to display all the ancestors and their attributes of a selected element? ai2003lian@yahoo.com XML 6 02-11-2005 06:35 PM
Multiply ancestors in xsl Martin Pettersson XML 2 10-18-2004 09:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57