How to compute a node's depth in XSLT?

Discussion in 'XML' started by datamodel, Dec 3, 2006.

  1. datamodel

    datamodel Guest

    Hello I have an XML tree of which you can see a mini-version here:

    http://paste.uni.cc/11838

    (the tree is actually over 30,000 levels deep)

    How do I count the depth of a given <NODES><NODE> ?????

    The problem is I would like to "color" nodes based on their depth in
    the tree - ie, use alternating colors.

    A depth, or actually "level" in the logical tree actually comprises two
    levels in the XML tree - ie
    <NODES><NODE>

    There is no depth attribute in the XML tags,
    and the position() function applies only to the currently processed
    node,

    Ideally I want to construct a test like

    <xsl:if test="[parent::position() &lt; 3]">

    of course this is wrong syntax.

    and position() doesn't help , since it really computes the position in
    the siblings at a given level,
    not the level of depth in the tree.
     
    datamodel, Dec 3, 2006
    #1
    1. Advertising

  2. datamodel

    datamodel Guest

    Re: How to compute a node's depth in XSLT? -- Solved

    This solution is in XSLT 1.0
    not very elegant to the time complexity -

    I used the function :

    count(ancestor::NODES)

    to determine a value for the depth of a given <NODE> node.


    datamodel wrote:
    > Hello I have an XML tree of which you can see a mini-version here:
    >
    > http://paste.uni.cc/11838
    >
    > (the tree is actually over 30,000 levels deep)
    >
    > How do I count the depth of a given <NODES><NODE> ?????
    >
    > The problem is I would like to "color" nodes based on their depth in
    > the tree - ie, use alternating colors.
    >
    > A depth, or actually "level" in the logical tree actually comprises two
    > levels in the XML tree - ie
    > <NODES><NODE>
    >
    > There is no depth attribute in the XML tags,
    > and the position() function applies only to the currently processed
    > node,
    >
    > Ideally I want to construct a test like
    >
    > <xsl:if test="[parent::position() &lt; 3]">
    >
    > of course this is wrong syntax.
    >
    > and position() doesn't help , since it really computes the position in
    > the siblings at a given level,
    > not the level of depth in the tree.
     
    datamodel, Dec 3, 2006
    #2
    1. Advertising

  3. Re: How to compute a node's depth in XSLT? -- Solved

    datamodel wrote:
    > count(ancestor::NODES)


    That counts the ancestors elements named <NODES>. I think you meant
    count(ancestor::node())


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Dec 3, 2006
    #3
  4. datamodel wrote:

    > Hello I have an XML tree of which you can see a mini-version here:
    >
    > http://paste.uni.cc/11838
    >
    > (the tree is actually over 30,000 levels deep)


    Thanks for pointing us to this data source.
    I actually downloaded the original file here:

    http://www.tolweb.org/tree/home.pages/downloadtree.html

    I was a bit disappointed by the crude quality
    of this data.

    xmllint --noout tolskeletaldump.xml
    tolskeletaldump.xml:25234: parser error : Input is not proper UTF-8, indicate encoding !
    Bytes: 0xE9 0x6B 0x65 0x73
    EXTINCT="0" ID="66855"><NAME><![CDATA[Stichotrema sz?essyi]]></NAME></NOD
    ^
    Whoever created this data, he should insert
    a proper XML header indictaing the encoding.

    > How do I count the depth of a given <NODES><NODE> ?????


    I am still interested in this XML file because it
    is the only example of real-life data I ever saw
    that claimed to contain 30,000 levels of depth.
    Last year I created a synthetic file with 10,000
    levels (for regression tests) and some people told
    me that this is nonsense. They said that data with
    so many levels never occur in real-life.

    I am interested in using your file for regression tests.
    Do you see any chance that your file will be supplied
    in an updated and corrected form ?
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Dec 3, 2006
    #4
  5. datamodel

    datamodel Guest

    I also got the file from the same location you cited.
    I am not the author of the file, and I agree with you.
    I was also disappointed at crudeness / quality of the document.
    However I was very happy that TOL made this file available to the
    public :)


    Jürgen Kahrs wrote:
    > datamodel wrote:
    >
    > > Hello I have an XML tree of which you can see a mini-version here:
    > >
    > > http://paste.uni.cc/11838
    > >
    > > (the tree is actually over 30,000 levels deep)

    >
    > Thanks for pointing us to this data source.
    > I actually downloaded the original file here:
    >
    > http://www.tolweb.org/tree/home.pages/downloadtree.html
    >
    > I was a bit disappointed by the crude quality
    > of this data.
    >
    > xmllint --noout tolskeletaldump.xml
    > tolskeletaldump.xml:25234: parser error : Input is not proper UTF-8, indicate encoding !
    > Bytes: 0xE9 0x6B 0x65 0x73
    > EXTINCT="0" ID="66855"><NAME><![CDATA[Stichotrema sz?essyi]]></NAME></NOD
    > ^
    > Whoever created this data, he should insert
    > a proper XML header indictaing the encoding.
    >
    > > How do I count the depth of a given <NODES><NODE> ?????

    >
    > I am still interested in this XML file because it
    > is the only example of real-life data I ever saw
    > that claimed to contain 30,000 levels of depth.
    > Last year I created a synthetic file with 10,000
    > levels (for regression tests) and some people told
    > me that this is nonsense. They said that data with
    > so many levels never occur in real-life.
    >
    > I am interested in using your file for regression tests.
    > Do you see any chance that your file will be supplied
    > in an updated and corrected form ?
     
    datamodel, Jan 16, 2007
    #5
  6. datamodel wrote:
    > I also got the file from the same location you cited.
    > I am not the author of the file, and I agree with you.
    > I was also disappointed at crudeness / quality of the document.
    > However I was very happy that TOL made this file available to the
    > public :)
    >
    >
    > Jürgen Kahrs wrote:
    >> datamodel wrote:
    >>
    >>> Hello I have an XML tree of which you can see a mini-version here:
    >>>
    >>> http://paste.uni.cc/11838
    >>>
    >>> (the tree is actually over 30,000 levels deep)

    >> Thanks for pointing us to this data source.
    >> I actually downloaded the original file here:
    >>
    >> http://www.tolweb.org/tree/home.pages/downloadtree.html
    >>
    >> I was a bit disappointed by the crude quality
    >> of this data.
    >>
    >> xmllint --noout tolskeletaldump.xml
    >> tolskeletaldump.xml:25234: parser error : Input is not proper UTF-8, indicate encoding !
    >> Bytes: 0xE9 0x6B 0x65 0x73
    >> EXTINCT="0" ID="66855"><NAME><![CDATA[Stichotrema sz?essyi]]></NAME></NOD
    >> ^
    >> Whoever created this data, he should insert
    >> a proper XML header indictaing the encoding.
    >>
    >>> How do I count the depth of a given <NODES><NODE> ?????

    >> I am still interested in this XML file because it
    >> is the only example of real-life data I ever saw
    >> that claimed to contain 30,000 levels of depth.
    >> Last year I created a synthetic file with 10,000
    >> levels (for regression tests) and some people told
    >> me that this is nonsense. They said that data with
    >> so many levels never occur in real-life.
    >>
    >> I am interested in using your file for regression tests.
    >> Do you see any chance that your file will be supplied
    >> in an updated and corrected form ?

    >


    if you stick
    <?xml version="1.0" encoding="iso-8859-1"?>
    at the top of the file extracted from the referenced zip file then it
    becomes well formed.

    executing the Xquery

    max(doc('tolskeletaldump2.xml')//*/count(ancestor::*))


    returns 241 as the maximum depth of an element node, which is rather
    less than 30000.

    David
     
    David Carlisle, Jan 16, 2007
    #6
  7. David Carlisle wrote:

    > if you stick
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > at the top of the file extracted from the referenced zip file then it
    > becomes well formed.


    Indeed, now it is well formed.

    > executing the Xquery
    >
    > max(doc('tolskeletaldump2.xml')//*/count(ancestor::*))
    >
    >
    > returns 241 as the maximum depth of an element node, which is rather
    > less than 30000.


    Yes, my software also calculates this value.
    I wonder why the OP claimed a depth of 30000.
     
    =?ISO-8859-1?Q?J=FCrgen_Kahrs?=, Jan 16, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,649
  2. Ralf Wahner
    Replies:
    2
    Views:
    2,467
    Ralf Wahner
    Jul 15, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,508
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    3
    Views:
    3,933
    A. Bolmarcich
    Oct 20, 2006
  5. PerlFAQ Server
    Replies:
    0
    Views:
    287
    PerlFAQ Server
    Feb 2, 2011
Loading...

Share This Page