Re: [xsl] Which XSL DTD to use and how do I find the depth of a node relati ve to the root?

Subject: Re: [xsl] Which XSL DTD to use and how do I find the depth of a node relati ve to the root?
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 22 Dec 2000 10:09:06 +0000
Hi Uri,

>   One is which XSL DTD should I use  - "http://www.w3.org/TR/WD-xsl";
> or "http://www.w3.org/1999/XSL/Transform";?   How are they different
> and which is newer/better?

You should be using http://www.w3.org/1999/XSL/Transform - it is the
namespace for XSLT 1.0, whereas http://www.w3.org/TR/WD-xsl is the
namespace from an early working draft and shouldn't be touched with a
barge pole.  They are namespaces rather than DTDs, though: just a
unique identifier for a different XML vocabulary, rather than actually
pointing to element definitions or anything.

>   My second question is what is the way to calculate the depth 
> (number of levels below the root) of a node in an XML document 
> using XSL?

The level of a node below the root node is the same as the number of
ancestor elements that the node has, plus one.  The document element
does not have any ancestor elements, so it has a level of 1, its
children have only it as an ancestor, and have a level of 2, and so
on.

You can find the ancestors of a node using the ancestor:: axis, and
select ancestor elements with any name using '*' as the node test.
You can count how many nodes this gives you using the count()
function.  So, to get the level of a node, use:

  count(ancestor::*)

You can store this number in a variable, or you can output it using
xsl:value-of.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread