The XPath node-set functions perform operations on (as the name implies) node-sets which have been located at the moment the function call is made.
In the following table, which summarizes these node-set functions, ns represents a node-set passed as an argument; ns? represents an optional node-set argument; and obj represents an object of some arbitrary type, such as node-set or string.
Format | Description/Example |
---|---|
last() | Returns the number of the last node in the currently selected node-set |
position() | Returns the number of the current node in the selected node-set. Note: [position()=some number] as an expression's predicate can be abbreviated as [some number]; for example, [position()=3] can be shortened to simply [3]. |
count(ns) | Returns the number of nodes in the node-set passed to the function. |
id(obj) | Returns the node with the ID-type attribute whose value equals that of obj; if the latter is a string, see the description of the string() function for how the value is converted. |
local-name(ns?) | Returns the "local name" of the argument. The full name of an element is considered to be its expanded namethat is, including the prefix associated with its namespace, if there is one. The local name is this "full name" with the namespace prefix omitted. |
namespace-uri(ns?) | Returns the URI associated with the argument's namespace, if there is one. |
name(ns?) | Returns the "full name" of an element, including its namespace prefix (if any). |
This location path:
/sales/region[last()
]/units
returns the number of units sold by the last region in the document, or 465.
The last()
function is similar to the count()
function in that both return the number of nodes in a given node-set. The difference is that last()
always operates on the node-set in effect at the time of the function call, while count()
requires that a node-set be supplied to it.
The location path:
/sales/region[position()
=2]/@name
returns the value of the name
attribute for the second region in the document, or "Southeast"
.
This XPath expression:
count(/sales/region)
returns the number of <region>
elements that are children of the root <sales>
element, or 4.
The count()
function is similar to the last()
function in that both return the number of nodes in a given node-set. The difference is that last()
always operates on the node-set in effect at the time of the function call, while count()
requires that a node-set be supplied to it.
You could determine the amount associated with sales in the Northwest region with the following:
//*[id("Northwest")
]/amount
This navigates down to the element of any name with an ID-type attribute whose value is "Northwest"
, and locates the value of the corresponding <amount>
element, or 12500.26.
The Sample XML Data File for XPath Functions doesn't include any namespace declarations. Even if it did, though, the following XSLT template:
<xsl:for-each select="//region[2]/*">
<xsl:value-of select="local-name()
"/>
</xsl:for-each>
would simply display the local names of all child elements of the second <region>
element as a single string, or "unitsamount"
.
Note that the local-name()
function always extracts the simple name(s) of a given node-set (the context node, by default), and therefore always works, regardless of the presence or absence of namespace declarations. The name()
function, on the other hand, always refers to nodes by their fully-qualified names, including any namespace prefixes.
Our sample document doesn't include any namespace declarations. However, consider the case of an XSLT style sheet which begins with the following <xsl:stylesheet>
element:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
If a separate style sheet were applied to this one, containing the following template rule:
<xsl:template match="/xsl:stylesheet">
<xsl:value-of select="namespace-uri()
"/>
</xsl:template>
the result would be the URI in effect for the <xsl:stylesheet>
element, or "http://www.w3.org/1999/XSL/Transform"
.
Our sample document doesn't include any namespace declarations. For this reason, all element names returned by the name()
function will be identical to those returned by the local-name()
function. However, consider an XSLT style sheet for transforming XML data to HTML; such a style sheet typically opens with this <xsl:stylesheet>
element:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="xmlns="http://www.w3.org/TR/REC-html40">
If you transformed this style sheet using a separate one containing the following template rule:
<xsl:template match="/xsl:stylesheet"> <pre> <p> local-name(): <xsl:value-of select="local-name()"/> </p> <p> name(): <xsl:value-of select="name()"/> </p> </pre> </xsl:template>
the output, when viewed in Internet Explorer, would be:
local-name(): stylesheet name(): xsl:stylesheet
Note that name()
returns the full name, including namespace prefix.
Sample XML Data File for XPath Functions | Processing Text Strings by Using String Functions