fn:path

Returns a path expression that can be used to select the supplied node relative to the root of its containing document.

Signatures

fn:path() as xs:string?
fn:path($arg as node()?) as xs:string?

Properties

The one-argument form of this function is deterministic, context-dependent, and focus-dependent.

The two-argument form of this function is deterministic, context-independent, and focus-independent.

Rules

The behavior of the function if the argument is omitted is exactly the same as if the context item (.) had been passed as the argument.

If $arg is the empty sequence, the function returns the empty sequence.

If $arg is a document node, the function returns the string "/".

Otherwise, the function returns a string that consists of a sequence of steps, one for each ancestor-or-self of $arg other than the root node. This string is prefixed by "Q{http://www.w3.org/2005/xpath-functions}root()" if the root node is not a document node. Each step consists of the character "/" followed by a string whose form depends on the kind of node selected by that step, as follows:

  1. For an element node, Q{uri}local[position], where uri is the namespace URI of the node name or the empty string if the node is in no namespace, local is the local part of the node name, and position is an integer representing the position of the selected node among its like-named siblings.

  2. For an attribute node:

    1. if the node is in no namespace, @local, where local is the local part of the node name

    2. otherwise, @Q{uri}local, where uri is the namespace URI of the node name, and local is the local part of the node name

  3. For a text node: text()[position] where position is an integer representing the position of the selected node among its text node siblings

  4. For a comment node: comment()[position] where position is an integer representing the position of the selected node among its comment node siblings

  5. For a processing-instruction node: processing-instruction(local)[position] where local is the name of the processing instruction node and position is an integer representing the position of the selected node among its like-named processing-instruction node siblings

  6. For a namespace node:

    1. If the namespace node has a name: namespace::prefix, where prefix is the local part of the name of the namespace node (which represents the namespace prefix).

    2. If the namespace node has no name (that is, it represents the default namespace): namespace::*[Q{http://www.w3.org/2005/xpath-functions}local-name()=""]

Error Conditions

The following errors may be raised when $arg is omitted:

Examples

let $e := 
document {            
<p xmlns="http://example.com/one" xml:lang="de" author="Friedrich von Schiller">
Freude, schöner Götterfunken,<br/>
Tochter aus Elysium,<br/>
Wir betreten feuertrunken,<br/>
Himmlische, dein Heiligtum.</p>}
         

The expression fn:path($e) returns '/'.

The expression fn:path($e/*:p) returns '/Q{http://example.com/one}p[1]'.

The expression fn:path($e/*:p/@xml:lang) returns '/Q{http://example.com/one}p[1]/@Q{http://www.w3.org/XML/1998/namespace}lang'.

The expression fn:path($e/*:p/@author) returns '/Q{http://example.com/one}p[1]/@author'.

The expression fn:path($e/*:p/*:br[2]) returns '/Q{http://example.com/one}p[1]/Q{http://example.com/one}br[2]'.

The expression fn:path($e//text()[starts-with(normalize-space(), 'Tochter')]) returns '/Q{http://example.com/one}p[1]/text()[2]'.

let $emp := 
            <employee xml:id="ID21256">
               <empnr>E21256</empnr>
               <first>John</first>
               <last>Brown</last>
            </employee>
         

The expression fn:path($emp) returns 'Q{http://www.w3.org/2005/xpath-functions}root()'.

The expression fn:path($emp/@xml:id) returns 'Q{http://www.w3.org/2005/xpath-functions}root()/@Q{http://www.w3.org/XML/1998/namespace}id'.

The expression fn:path($emp/empnr) returns 'Q{http://www.w3.org/2005/xpath-functions}root()/Q{}empnr[1]'.