0% found this document useful (0 votes)
13 views

A Primer on XPath Functions

This document serves as a primer on XPath functions, detailing their categories: node, string, boolean, and number functions. It provides examples of how to use these functions to manipulate XML data, including selecting nodes, manipulating strings, and performing calculations. Additionally, it discusses the role of XPath in conjunction with technologies like XSLT and XPointer for navigating XML documents.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

A Primer on XPath Functions

This document serves as a primer on XPath functions, detailing their categories: node, string, boolean, and number functions. It provides examples of how to use these functions to manipulate XML data, including selecting nodes, manipulating strings, and performing calculations. Additionally, it discusses the role of XPath in conjunction with technologies like XSLT and XPointer for navigating XML documents.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

8/16/23, 9:31 AM [30 min] READ: A Primer on XPath Functions: CS5200 DBMS MERGED Summer 2023

[30 min] READ: A Primer on XPath


Functions
Using XPath Functions: A Primer
This primer looks at XPath functions and goes into a little more detail regarding their use in creating
expressions. Before getting into the specifics of the XPath functions at your disposal, it's worth taking
a look at their general use. The functions supported by XPath, which are available for use in creating
XPath expressions, can be roughly divided along the lines of the data types on which they operate:

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:

position() Determine the numeric position of a node

last() Determine the last node in a node set

count() Determine the number of nodes in a node set

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:

concat() Concatenate two strings together

starts-with() Determine if a string begins with another string

contains() Determine if a string contains another string

substring-before() Retrieve a substring that appears before another string

substring-after() Retrieve a substring that appears after another string

substring() Retrieve a substring of a specified length starting at an index within another string

string-length() Determine the length of a 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:

not() Negate a Boolean value

lang() Determine if a certain language is being used

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

ceiling() Round up a decimal value to the nearest integer

floor() Round down a decimal value to the nearest integer

round() Round a decimal value to the nearest integer

sum() Add a set of numeric values

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))

The Role of XPath


You may have noticed that I've used the word "select" a lot in this tutorial when explaining how an
XPath expression effectively selects part of a document. However, this selection process doesn't
take place within XPath alone. XPath is always used in the context of another technology such as
XSLT, XPointer, or XLink. The examples of XPath that you've seen in this lesson must therefore be
used in conjunction with additional code. For example, the following code shows how one of the
training log expressions from earlier in the tutorial might be used in an XSLT stylesheet:

<xsl:value-of select="*/session[@type='running']" />

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy