473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML: Collecting ancestors

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
Jul 20 '05 #1
2 3370
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

<xsl:output 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 -->
<xsl:otherwise>
<xsl:value-of select="0"/>
</xsl:otherwise>
</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

Jul 20 '05 #2
Ed Beroset <be*****@mindspring.com> wrote in message news:<fj***************@newsread1.news.atl.earthli nk.net>...
That should give you some ideas.

Ed


Thank You!!!!!
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim | last post by:
Hi I'm looking to take an existing XML document, query for certain nodes, and 'recreate' the document with just the relevant nodes. I'm currently using XPath - I have established the pattern that...
2
by: Michael C | last post by:
Hi all, I'm playing around with an XML structured like this: <root> <subone> <setting name = "xyz" value = "100" /> </subone> <subtwo> <list name = "abcde" value = "1000" />
4
by: Christian Rühl | last post by:
Good Day, folks! I'm having a problem traversing an XmlDocument tree in C#. I only want to access the InnerText of the leafs and the names of their ancestors and show them in a richTextBox. But...
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
4
by: andersch | last post by:
Hi I display a xml file with the TreeView control (Windows Forms) on my MainForm. Now, I would like to be able to edit this xml file (XmlElements) when I select a node in this TreeView. For...
6
by: Dan | last post by:
I'm using python's xml.dom.minidom module to generate xml files, and I'm running into memory problems. The xml files I'm trying to create are relatively flat, with one root node which may have...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
1
by: Author | last post by:
I have an xml document that looks *like* so: <Mammal name="Cat"> <Mammal name="African Tigers" > <Mammal name="Central African Tiger" /> <Mammal name="Ethiopian Tiger" /> </Mammal> <Mammal...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.