A Primer on XPath Functions
A Primer on XPath Functions
Node functions
String functions
Boolean functions
Number functions
The next few sections explore the functions in each of these categories in more detail. For a
complete XPath function reference, please visit the XPath page at the W3C web site
at http://www.w3.org/TR/xpath#corelib .
Node Functions
Node functions are XPath functions that relate to the node tree. Although all of XPath technically
relates to the node tree, node functions are very direct in that they allow you to ascertain the position
of nodes in a node set, as well as how many nodes are in a set. Following are the most common
XPath node functions:
Although these node functions might seem somewhat abstract, keep in mind that they can be used to
carry out some interesting tasks when used in the context of a broader expression. For example, the
following code shows how to use the count() function to calculate the total distance in the training
log document for sessions whose distances are recorded in miles:
count(*/distance[@units='miles'])
https://northeastern.instructure.com/courses/146257/pages/30-min-read-a-primer-on-xpath-functions?module_item_id=8783926 1/5
8/16/23, 9:31 AM [30 min] READ: A Primer on XPath Functions: CS5200 DBMS MERGED Summer 2023
Following is another example that shows how to reference a child node based solely upon its position
within a document:
child::item[position()=3]
Assuming there are several child elements of type item , this code references the third
child item element of the current context. To reference the last child item, you use
the last() function instead of an actual number, like this:
child::item[position()=last()]
String Functions
The XPath string functions are used to manipulate strings of text. With the string functions you can
concatenate strings, slice them up into substrings, and determine the length of them. Following are
the most popular string functions in XPath:
substring() Retrieve a substring of a specified length starting at an index within another string
These XPath string functions can come in quite handy when it comes to building expressions,
especially when you consider that XML content is always specified as raw text. In other words, it is
possible to manipulate most XML content as a string, regardless of whether the underlying value of
the content is numeric or some other data type. Following is an example that demonstrates how to
extract the month of a training session from a date attribute in the training log document:
substring-after(/session[1]@date, "/")
In this example, the substring-after() function is called and passed the date attribute. Because a
forward slash ( / ) is passed as the second argument to the function, it is used as the basis for
finding the substring. If you look back at one of the date attributes in the document (line 6, for
example), you'll notice that the month appears just after the first forward slash. As a comparison, you
https://northeastern.instructure.com/courses/146257/pages/30-min-read-a-primer-on-xpath-functions?module_item_id=8783926 2/5
8/16/23, 9:31 AM [30 min] READ: A Primer on XPath Functions: CS5200 DBMS MERGED Summer 2023
could extract the year as a substring by providing the same arguments but instead using
the substring-before() function:
substring-before(/session[1]@date, '/')
Another use of the string functions is finding nodes that contain a particular substring. For example, if
you wanted to analyze your training data and look for training sessions where you felt strong, you
could use the contains() function to select session elements where the comments child element
contains the word "strong":
*/session[contains(comments, 'strong')]
In this example, the second and third session elements would be selected because they both contain
the word "strong" in their comments child elements (lines 17 and 24).
Boolean Functions
Boolean functions are pretty simple in that they operate solely on Boolean (true/false) values.
Following are the two primary Boolean functions that you may find useful in XPath expressions:
The not() function is pretty straightforward in that it simply reverses a Boolean value: true becomes
false and false becomes true. The lang() function is a little more interesting because it actually
queries a node to see what language it uses. As an example, many English-language XML
documents set the xml:lang attribute to en in the root element. Although this value typically
cascades down to all elements within the document, it's possible for a document to use multiple
languages. The lang() function allows you to check the language setting for any node. Following is
an example of how to use the not() and lang() functions to determine if the English language is not
being used in a document:
not(lang("en"))
Number Functions
The XPath number functions should be somewhat familiar to you since you saw them in action back
in Tutorial 13
(https://www.brainbell.com/tutorials/XML/TOC_Access_Your_ITunes_Music_Library_Via_XML.htm)
when you created XSLT stylesheets that relied on the number functions. Following are the most
commonly used number functions in XPath:
https://northeastern.instructure.com/courses/146257/pages/30-min-read-a-primer-on-xpath-functions?module_item_id=8783926 3/5
8/16/23, 9:31 AM [30 min] READ: A Primer on XPath Functions: CS5200 DBMS MERGED Summer 2023
Following is an example of how to use the sum() function to add up a bunch of attribute values:
sum(cart/item/@price)
Of course, you can make nested calls to the XPath number functions. For example, you can round
the result of the sum() function by using the round() function, like this:
round(sum(cart/item/@price))
In this code, the XPath expression appears within the select attribute of the xsl:value-of element,
which is responsible for inserting content from a source XML document into an output document
during the transformation of the source document. Refer back to Tutorials 12
(https://www.brainbell.com/tutorials/XML/TOC_Transforming_XML_With_XSLT.htm) and 13
(https://www.brainbell.com/tutorials/XML/TOC_Access_Your_ITunes_Music_Library_Via_XML.htm) for
more information on XSLT stylesheets and how they are used. The point I want to make here is that
the XSLT xsl:value-of element is what makes the XPath expression useful. XPath plays a critical
role in XSLT, as you probably remember from Tutorial 13
(https://www.brainbell.com/tutorials/XML/TOC_Access_Your_ITunes_Music_Library_Via_XML.htm) .
Similar to its role in XSLT, XPath serves as the addressing mechanism in XPointer. XPointer is used
to address parts of XML documents, and is used heavily in XLink, which you learn about in a
moment. XPointer uses XPath to provide a means of navigating the tree of nodes that comprise an
XML document. Sounds familiar, right? XPointer takes XPath a step further by defining a syntax for
https://northeastern.instructure.com/courses/146257/pages/30-min-read-a-primer-on-xpath-functions?module_item_id=8783926 4/5
8/16/23, 9:31 AM [30 min] READ: A Primer on XPath Functions: CS5200 DBMS MERGED Summer 2023
fragment identifiers, which are in turn used to specify parts of documents. In doing so, XPointer
provides a high degree of control over the addressing of XML documents.
[This page was adapted from Using XPath Functions : XML (brainbell.com)
(https://www.brainbell.com/tutorials/XML/Using_XPath_Functions.htm#:~:text=The%20XPath%20string%
20functions%20are%20used%20to%20manipulate,functions%20in%20XPath%3A%20concat%28%29%20
Concatenate%20two%20strings%20together.) ]
https://northeastern.instructure.com/courses/146257/pages/30-min-read-a-primer-on-xpath-functions?module_item_id=8783926 5/5