Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 7th, 2011, 04:04 PM
Registered User
 
Join Date: Jan 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default depth of inheritance tree

Hi

How can get the depth of inheritance tree?

I have a xml like this:

<class><name>ColumnX</name>
<super><name>Static</name></super>
</class>
<class><name>ColumnY</name>
<super><name>ColumnX</name></super>
</class>
<class><name>ColumnZ</name>
<super><name>ColumnY</name></super>
</class>
<class><name>ColumnW</name>
<super><name>Static</name></super>
</class>

I'm working a xsl like this:

<xsl:template match="//class">
<xsl:apply-templates select="//*[name()='super' and super/name = class/name]"/>
</xsl:template>

But, is this is a disaster. I do not know how to get the highest level of inheritance depth of an XML tree as above.

Any ideas?
 
Old February 7th, 2011, 06:49 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I would start by building the tree as an XML tree, and then find the maximum depth. (Presumably you want the tree for other reasons as well.) By the "XML tree" I mean one where the parent-child relationship in the tree represents the superclass-subclass relationship in the data. Assuming you know what the root node is, building the tree is not difficult: it's essentially

Code:
<xsl:template match="class">
  <class name="{name}">
    <xsl:apply-templates select="//class[super/name=current()/name]"/>
  </class>
</xsl:template>
Then the maximum depth in this tree is max(//class/count(ancestor::*)).
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 7th, 2011, 08:19 PM
Registered User
 
Join Date: Jan 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

My xslt do not support max function!!

Is there another way to do it?

Thanks.
 
Old February 8th, 2011, 05:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you're constrained to use XSLT 1.0 then it's a good idea to say so, since it avoids wasting people's time (usually I leave XSLT 1.0 questions to someone else since it's so long since I used it, and since the answers are often so convoluted).

Generally the way to compute max() in XSLT 1.0 is to sort into descending order, and then take the first. Essentially

Code:
<xsl:for-each select="//*">
  <xsl:sort select="count(ancestor::*)" data-type="number" order="descending"/>
  <xsl:if test="position() = 1">
    <xsl:value-of select="count(ancestor::*)"/>
  </xsl:if>
</xsl:for-each>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Node Depth mohamed_eldib XSLT 1 June 17th, 2010 04:12 AM
decrease recursive depth scubin XSLT 2 January 8th, 2010 11:04 PM
Bitmap color-depth issue warlock7 Visual C++ 2005 0 July 25th, 2007 12:55 PM
Image Color depth and mode qazi_nomi ASP.NET 1.0 and 1.1 Professional 3 January 24th, 2007 02:49 PM
Depth First Search gbilios C++ Programming 3 June 8th, 2004 12:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.