functx:depth-of-node

The depth (level) of a node in an XML tree

Description

The functx:depth-of-node function returns the level of a node within a document, as an integer. 1 represents the root node, 2 is a child of the root, etc. If $node is a document node, the document node itself counts as the first level.

Parameters

Description

node:node()

the node to check

Examples

The examples below use this input document:

<xsl:variable name="in-xml" as="item()*">
  <authors>
     <author>
        <fName>Kate</fName>
        <lName>Jones</lName>
     </author>
     <author>
        <fName>John</fName>
        <lName>Doe</lName>
     </author>
  </authors>

</xsl:variable>

XPath

Results

functx:depth-of-node($in-xml)

1

functx:depth-of-node($in-xml/author[1])

2

functx:depth-of-node($in-xml/author[1]/fName/text())

4

In a generator

Copy the snippet below and paste it in Freemapper, under a language node inside the generators element. Each line of the pattern prints the result of one example; to see them, run mi generate <project> <language> example-functx-depth-of-node.

[e] example-functx-depth-of-node
    [e] vars
        [e] in-xml
            [e] select
                [v] copy-of(parse-xml('<authors><author><fName>Kate</fName><lName>Jones</lName></author><author><fName>John</fName><lName>Doe</lName></author></authors>')/*)
    [e] start
        [e] match
            [v] /
        [e] body
            [e] write-pattern
                [v] examples
    [e] patterns
        [e] examples
            [v] E01: {{ functx:depth-of-node($in-xml) }}
            [v] E02: {{ functx:depth-of-node($in-xml/author[1]) }}
            [v] E03: {{ functx:depth-of-node($in-xml/author[1]/fName/text()) }}

See Also