Node functions report a node's identity and content: name(), local-name(), namespace-uri() and node-name() (Namespaces and xml: Attributes), root(), base-uri() and document-uri(), string() (all descendant text joined) and data() (the typed value). XPath 3.0 added path(), which writes an absolute path to any node, has-children(), and innermost() and outermost(), which drop nodes nested inside other nodes of the same sequence:
xp 'local-name((//price)[1])' 'path(//book[2]/title)' \
'tokenize(document-uri(/), "/")[last()]' 'string(//book[1]/supply)' \
'normalize-space(//book[1]/supply)' \
'innermost(//book[1]/supply/descendant-or-self::*) ! name()' 'generate-id(//book[1])'local-name((//price)[1]) => "price"
path(//book[2]/title) => "/Q{}catalog[1]/Q{}book[2]/Q{}title[1]"
tokenize(document-uri(/), "/")[last()] => "booknest-catalog.xml"
string(//book[1]/supply) => " in-stock 14.99 "
normalize-space(//book[1]/supply) => "in-stock 14.99"
innermost(//book[1]/supply/descendant-or-self::*) ! name() => "availability" "price"
generate-id(//book[1]) => "d0e13"path() writes each step as Q{namespace}name[position] (empty braces here, as the catalog has no namespace), which makes it ideal for error reports that must locate a bad value in a large feed. string() on an element concatenates its text nodes, indentation included (the xp wrapper shows the embedded newlines as spaces), so clean it with normalize-space(). generate-id() returns a key that is stable only within one run, so never store it as an identifier.
Node Functions
Node functions return information about nodes in the XDM tree — their names, namespace, base URI, string value, structural path, and other properties. The examples below evaluate against this sample document, an extended version of the <menu> example used in Path Expressions, here with an added xml:lang attribute, an ns namespace prefix, and an xml:id on the first <dish>:|
<?xml version="1.0"?> <menu restaurant="Golden Outlet" xmlns:ns="http://example.com" xml:lang="en"> <dish xml:id="1">Satay <i>Cooked</i></dish> <drink id="2">Cola</drink> <dish id="3">Curry Chicken</dish> <dish id="4" xmlns="http://example.com">Sweet and Sour Pork</dish> </menu> |
Each of the following returns the string dish:
| name(menu/ns:dish) |
| menu/dish[1]/name() |
| node-name(menu/ns:dish) |
| local-name(menu/ns:dish) |
| id("1")/name() |
| element-with-id("1")/name() |
| idref("1")/name() |
namespace-uri(menu/ns:dish) returns http://example.com.
Each of the following returns the base URI in effect for the evaluation context (e.g. /home/project-web/videlibri/cgi-bin/ for the online evaluator referenced in XPath):
| base-uri(menu/ns:dish) |
| base-uri() |
| static-base-uri() |
Each of the following returns an empty sequence, since the document was not loaded with doc() and therefore has no document URI:
| document-uri() |
| document-uri(menu) |
Each of the following returns the same string — the concatenated string value of every descendant text node of the document, including the whitespace between elements: Satay Cooked Cola Curry Chicken Sweet and Sour Pork
| string() |
| string(menu) |
| data(menu) |
Each of the following returns /, the structural path of the current node from the document root:
| path() |
| path(.) |
innermost(menu) returns the nodes in a node sequence that have no descendant node also present in that sequence — for the sample document: <i>Cooked</i>, <drink id="2">Cola</drink>, <dish id="3">Curry Chicken</dish>, and <dish id="4" xmlns="http://example.com">Sweet and Sour Pork</dish>.
outermost(menu/dish/i) returns the nodes in a node sequence that are not descendants of any other node in that sequence:
|
<?xml version="1.0"?> <menu restaurant="Golden Outlet" xmlns:ns="http://example.com"> <dish id="1">Satay <i>Cooked</i></dish> <drink id="2">Cola</drink> <dish id="3">Curry Chicken</dish> <dish id="4" xmlns="http://example.com">Sweet and Sour Pork</dish> </menu> |
nilled(menu) returns false, since the element has content and is not marked xsi:nil="true".
Each of the following returns true:
| has-children() |
| has-children(menu) |
| lang("en") |
| lang("en",menu/dish) |
generate-id() returns an implementation-dependent unique identifier string for the current node; the exact value is not standardized and differs between XPath processors.
<?xml version="1.0"?>
<menu restaurant="Golden Outlet" xmlns:ns="http://example.com" xml:lang="en">
<dish xml:id="1">Satay <i>Cooked</i></dish>
<drink id="2">Cola</drink>
<dish id="3">Curry Chicken</dish>
<dish id="4" xmlns="http://example.com">Sweet and Sour Pork</dish>
</menu>
namespace-uri(menu/ns:dish)
http://example.com