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:
-
For an element node,
Q{uri}local[position]
, whereuri
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, andposition
is an integer representing the position of the selected node among its like-named siblings. -
For an attribute node:
-
if the node is in no namespace,
@local
, wherelocal
is the local part of the node name -
otherwise,
@Q{uri}local
, whereuri
is the namespace URI of the node name, andlocal
is the local part of the node name
-
-
For a text node:
text()[position]
whereposition
is an integer representing the position of the selected node among its text node siblings -
For a comment node:
comment()[position]
whereposition
is an integer representing the position of the selected node among its comment node siblings -
For a processing-instruction node:
processing-instruction(local)[position]
wherelocal
is the name of the processing instruction node andposition
is an integer representing the position of the selected node among its like-named processing-instruction node siblings -
For a namespace node:
-
If the namespace node has a name:
namespace::prefix
, whereprefix
is the local part of the name of the namespace node (which represents the namespace prefix). -
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:
-
If the context item is absent, dynamic error [ERRXPDY0002]
-
If the context item is not a node, type error [ERRXPTY0004].
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]'
.