Home > Articles > Web Services > XML

Location Steps and Paths in XPath

The idea behind XPath location paths is very much the same as directory paths, except that the XPath syntax can get much more complex and detailed. Steve Holzner explains.
This chapter is from the book

Chapter 3: Location Steps and Paths

In this chapter

  • Understanding Location Steps and Paths

  • Using the XPath Axes

  • Creating Compound Location Paths

  • In Brief

Understanding Location Steps and Paths

How do location paths work? We took a look at location paths in the overview in Chapter 1, where we saw that location paths look much like directory paths. For example, you might store section one of chapter one of part one of a novel in a directory with this path if you're using Windows:

\novel\part1\section1\chapter1

or this path if you're using Unix:

/novel/part1/section1/chapter1

The idea behind XPath location paths is very much the same except that, as we'll see in this chapter and the next, the XPath syntax can get much more complex and detailed than directory paths. Like directory paths, you build location paths out of individual steps, called location steps, separated by / or //. You use these steps to specify the data you want, just as you use successive directories in a file path to get to the location of a file.

For example, in the XPath location path /library/book/title[2]/text(), we begin with a /, which matches the root node of the document, followed by, in order:

  • The location step library, which matches the child <library> element of the root node

  • The location step book, which matches the child <book> element of the <library> element

  • The location step title[2], which matches the second <title> element of the <book> element

  • text(), which matches the text in the <title> element

In fact, the XPath location path /library/book/title[2]/text() uses abbreviated XPath syntax, which we're going to see in this chapter. Here's the full version of this XPath location path, where the child element nature of each successive location step is spelled out:

/child::library/child::book/child::title[position()=2]/child::text

Okay—now let's get to the details.

The Parts of a Location Step

Each location step is made up of an axis, a node test, and zero or more predicates, like this (where the * symbol means "zero or more of"):

axis :: node-test [predicate]*

For example, in the location step child::name[position() = 2], child is the name of the axis (which you follow with ::), name is the node test and [position() = 2] is the predicate. In this case, the child axis selects child nodes of the node you start at, which is the context node. We're going to take a look at axes, node tests, and predicates in detail in this chapter. This location step can also be abbreviated, using the abbreviated syntax we'll see in this chapter, as name[2] (this works because the child axis is the default axis, and when you use position predicates like [position() = 2], XPath lets you abbreviate them as simply [2]).

Location paths can be made up of one or more location steps, such as child::name[position() = 2]/child::firstName, which selects all the <firstName> elements that are children of the second <name> child element of the context node. This location path can also be abbreviated as name[2]/firstName.

When you start the location path (not a location step) with / or //, the location path is called an absolute location path because you're specifying the path from the root node of the XML document (just as starting a file path with / in Unix starts the path from the root directory and makes the path an absolute one). Otherwise, the location path is relative, starting with the context node.

For example, take a look at this node tree again:

                     root
                       |
               element: <library>
                       |
                 element: <book>
                       |
              |-----------------------|
              |                       |
         element: <title>    element: <title>
              |                       |
      text: "I Love XPath"    text: "XPath is the BEST" 

Beginning a location path with / (or //, as we'll see) makes that path start with the document's root node, making this an absolute location path and making the root node the context node for the first location step in the path. To move to the child <library> element, you add the location step library like this: /library (using unabbreviated syntax, that would be /child::library). That makes the context node for the next location step the <library> element. To move to the <book> element starting from the <library> context node, you add book (or child::book) to the location path like this: /library/book and so on, all the way down to the text in the second <title> element, which you reach with /library/book/title[2]/text(). These are all absolute location paths, starting from the root node.

On the other hand, if you've got a context node already set—some XPath software lets you specify context nodes—your location paths can be relative. For example, if you've set the context node to the <library> element, the relative location path book/title[2]/text() will locate the text in the second <title> element in the document.

Now it's time to get systematic about our study of location steps, which we're going to do by taking a look at what kind of axes, node tests, and predicates XPath are available.

XPath Axes

We're going to take a look at all 13 XPath 1.0 axes in this chapter. For example, in the location step child::planet, which refers to all <planet> elements that are children of the context node, child is the axis (and as you now know, you can also abbreviate this location step as planet, because child is the default axis). Here are all the XPath 1.0 axes:

  • The ancestor axis holds the ancestors of the context node; the ancestors of the context node are the parent of context node and the parent's parent and so forth, back to and including the root node.

  • The ancestor-or-self axis holds the context node and the ancestors of the context node.

  • The attribute axis holds the attributes of the context node.

  • The child axis holds the children of the context node.

  • The descendant axis holds the descendants of the context node. A descendant is a child or a child of a child and so on.

  • The descendant-or-self axis contains the context node and the descendants of the context node.

  • The following axis holds all nodes in the same document as the context node that come after the context node.

  • The following-sibling axis holds all the following siblings of the context node. A sibling is a node on the same level as the context node.

  • The namespace axis holds the namespace nodes of the context node.

  • The parent axis holds the parent of the context node.

  • The preceding axis contains all nodes that come before the context node.

  • The preceding-sibling axis contains all the preceding siblings of the context node. A sibling is a node on the same level as the context node.

  • The self axis contains the context node.

Each XPath location step must specify an axis (or use the default child axis), as in this location path we've already seen: /child::library/child::book/child::title[2]/child::text.

You can also see the various XPath axes listed in Table 3.1, which lists the support for these axes by XML processors in Internet Explorer. The XML processor in Internet Explorer is called MSXML; MSXML 2.0 was the version in Internet Explorer 5.0, early versions of Internet Explorer 6.0 used MSXML3, and the current version is MSXML4. If you're using the .NET platform, your version of MSXML is MSXML.NET.

Table 3.1 The XPath Axes

Axis

Abbreviation

MSXML2

MSXML3

MSXML4

MSXML.NET

ancestor

 

 

x

x

x

ancestor-or-self

 

 

x

x

x

attribute

@

 

x

x

x

child

(default)

x

x

x

x

descendant

//

x

x

x

x

descendant-or-self

 

x

x

x

x

following

 

 

x

x

x

following-sibling

 

x

x

x

 

namespace

 

 

x

x

x

parent

..

x

x

x

x

preceding

 

 

x

x

x

preceding-sibling

 

x

x

x

 

self

 

x

x

x

x


XPath Node Tests

When you use an axis in a location step, you're telling XPath where to look and identifying a set of nodes. A node test tells XPath which of the nodes in that set you're interested in.

There are a number of ways to create node tests. You can use names of nodes as node tests, or the wildcard * to select element or attribute nodes (note especially that * matches only elements and attributes, not just any kind of node). For example, the location step child::*/child::name selects all <name> elements that are grandchildren of the context node. To match attributes, you'd use the attribute axis like this: attribute::*. Besides node names and the wildcard character, you can also use these node tests in XPath 1.0:

  • The * wildcard character matches any element or attribute name.

  • A name matches a node with that name (for example, planet will match a <planet> element).

  • The comment() node test selects comment nodes.

  • The node() node test selects any type of node.

  • The processing-instruction() node test selects a processing instruction node. You can specify the name of the processing instruction to select in the parentheses.

  • The text() node test selects a text node.

You can see the XPath 1.0 node tests in Table 3.2, along with the XML processor version that supports them in the Internet Explorer.

Table 3.2 The XPath Node Tests

Axis

MSXML2

MSXML3

MSXML4

MSXML.NET

*

x

x

x

x

name

x

x

x

x

comment()

x

x

x

x

node()

x

x

x

x

processing-instruction()

x

x

x

x

text()

x

x

x

x


The node test lets you specify what nodes you want to work with in an XPath location step. For example, take a look at our sample node tree:

                       root
                         |
                 element: <library>
                         |
                  element: <book>
                         |
              |---------------------|
              |                     |
         element: <title>       element: <title>
              |                     |
      text: "I Love XPath"    text: "XPath is the BEST"

You can start at the root node with /, and then use the child axis and the node test library to move to the <library> element—giving you the location path /child::library. You can see this at work in the XPath Visualiser in Figure 3.1.

Figure 3.1Figure 3.1 Using the XPath location step /child::library.

And you can move to the <book> element under the <library> element with another location step involving this child axis and a node test like this: /child:library/child:book. You can see what this looks like in the XPath Visualiser in Figure 3.2.

Figure 3.2Figure 3.2 Using the XPath location step /child::library/child::book.

The next level down in the node tree holds two <title> elements, however. What if we only want to work with the second one? If we used the location path /child::library/child::book/child::title, we'd match both <title> elements, so we need more than a node test here—we need to use a predicate.

XPath Predicates

The next part of a location step, which follows the node text, is the predicate. A location step doesn't need a predicate, but if you use a predicate, you can specify even more about the node or nodes you want to match.

You often use one of the built-in XPath functions in predicates. For example, take a look at the location step child::planet[position() = 2]. In this case, the predicate, which is always enclosed between [ and ], is position() = 2. This means that the value the built-in XPath function position() returns must indicate that this is the second <planet> child in order for the location step to match (this location step can also be abbreviated as planet[2]). In this way, this predicate narrows down the search from the node-set of all <planet> children of the context node down to the second <planet> child.

Now we're in a position to select the second <title> element in our XML document that has this node tree:

                          root
                            |
                      element: <library>
                            |
                      element: <book>
                            |
              |---------------------------|
              |                           |
         element: <title>            element: <title>
              |                           |
      text: "I Love XPath"        text: "XPath is the BEST"

To move to the second <title> element, we can use the location path /child::library/child::book/child::title[2], as you see in the XPath Visualiser in Figure 3.3 (this location path can also be abbreviated as /library/book/title[2]).

As you can see, expressions in predicates can let you narrow down the search from a whole node-set to just the nodes you're looking for.

Here's another node-test example—to select the <title> element that contains the text "I Love XPath", you can use this XPath location path with the text() node test: /child::library/child::book/child::title[text()="I Love XPath"], as you see in Figure 3.4.

Figure 3.3Figure 3.3 Using the location step /child::library/child::book/child::title[2].

Figure 3.4Figure 3.4 Selecting a <title> element.

Now we're able to construct XPath location paths like /child::library/child::book/ child::title[2] using axes, node tests, and predicates. Each part of a location step, the axis, node test, and predicate, narrows down the set of nodes you're working with.

You can also build up XPath expressions much as you can location paths. For example, the text() node test will return the text in a node, so if you want to extract the text of the <title> element, you can use the XPath expression /child::library/child::book/child::title[2]/text(), which evaluates not to a node-set, but to a text string, "XPath is the BEST", as you can see in Figure 3.5.

You can use multiple predicates in the same location step—for example, say that we added a language attribute to each <planet> element in our planetary data XML document, as you see in ch03_01.xml (see Listing 3.1).

Figure 3.5Figure 3.5 Using /child::library/child::book/child::title[2]/text().

Listing 3.1 Adding a Language Attribute (ch03_01.xml)

<?xml version="1.0" encoding="utf-8"?>
<planets>

<planet language="English">

    <name>Mercury</name>
    <mass units="(Earth = 1)">.0553</mass>
    <day units="days">58.65</day>
    <radius units="miles">1516</radius>
    <density units="(Earth = 1)">.983</density>
    <distance units="million miles">43.4</distance><!--At perihelion-->
  </planet>

<planet language="English">

    <name>Venus</name>
    <mass units="(Earth = 1)">.815</mass>
    <day units="days">116.75</day>
    <radius units="miles">3716</radius>
    <density units="(Earth = 1)">.943</density>
    <distance units="million miles">66.8</distance><!--At perihelion-->
  </planet>

<planet language="English">

    <name>Earth</name>
    <mass units="(Earth = 1)">1</mass>
    <day units="days">1</day>
    <radius units="miles">2107</radius>
    <density units="(Earth = 1)">1</density>
    <distance units="million miles">128.4</distance><!--At perihelion-->
  </planet>

</planets>

What if we wanted to reach the second <planet> element that has a language attribute set to "English"? We could do that with a location path like this: /planets/planet[attribute::language = "English"][position() = 2], as you see in Figure 3.6. In this way, you can handle multiple conditions with multiple predicates.

Figure 3.6Figure 3.6 Using /planets/planet[attribute::language = "English"][position() = 2].

Some Examples of XPath Location Paths

There's nothing like seeing all this at work to understand what's going on, so here are a number of location path examples:

  • child::planet—Returns the <planet> element children of the context node.

  • child::*—Returns all element children (* only matches elements, or attributes if you use it with the attribute axis) of the context node.

  • child::text()—Returns all text node children of the context node.

  • child::node()—Returns all the children of the context node, no matter what their node type is.

  • attribute::units—Returns the units attribute of the context node.

  • descendant::planet—Returns the <planet> element descendants of the context node.

  • ancestor::planet—Returns all <planet> ancestors of the context node.

  • ancestor-or-self::planet—Returns the <planet> ancestors of the context node. If the context node is a <planet> as well, also returns the context node.

  • descendant-or-self::planet—Returns the <planet> element descendants of the context node. If the context node is a <planet> as well, also returns the context node.

  • self::planet—Returns the context node if it is a <planet> element.

  • child::name/descendant::planet—Returns the <planet> element descendants of the child <name> elements of the context node.

  • child::*/child::planet—Returns all <planet> grandchildren of the context node.

  • /—Returns the root node (that is, the parent of the document element).

  • /descendant::planet—Returns all the <planet> elements in the document.

  • /descendant::planet/child::name—Returns all the <name> elements that have a <planet> parent.

  • child::planet[position() = 3]—Returns the third <planet> child of the context node.

  • child::planet[position() = last()]—Returns the last <planet> child of the context node.

  • /descendant::planet[position() = 3]—Returns the third <planet> element in the document.

  • child::planets/child::planet[position() = 4 ]/child::name[position() = 3]—Returns the third <name> element of the fourth <planet> element of the <planets> element.

  • child::planet[position() > 3]—Returns last() location path> last()> all the <planet> children of the context node after the first three.

  • preceding-sibling::name[position() = 2]—Returns the second previous <name> sibling element of the context node.

  • child::planet[attribute::color = "RED"]—Returns all <planet> children of the context node that have a color attribute with value of "RED".

  • child::planet[attribute::color = "RED"][position() = 3]—Returns the third <planet> child of the context node that has a color attribute with value of "RED".

  • child::planet[position() = 3][attribute::color="RED"]—Returns the third <planet> child of the context node, only if that child has a color attribute with value of "RED".

  • child::planet[child::name]—Returns the <planet> children of the context node that have <name> children.

As you can see, some of this syntax is pretty involved, and a little lengthy to type. However, there is an abbreviated form of XPath syntax, and we'll look at that next.

Using XPath Abbreviated Syntax

There are a number of abbreviations you can take advantage of in XPath syntax. Here are the rules:

  • self::node() can be abbreviated as .

  • parent::node() can be abbreviated as ..

  • child::nodename can be abbreviated as nodename

  • attribute::nodename can be abbreviated as @nodename

  • /descendant-or-self::node()/ can be abbreviated as //

You can also abbreviate predicate expressions like [position() = 3] as [3]. Using the abbreviated syntax makes XPath expressions a lot easier to write. For example, attribute::units can be abbreviated as @units, you can refer to the context node itself as simply ., and you can refer to the current node and any descendants as //.

The // syntax in particular is useful and important. Take a look at ch03_01.xml, for example, the XML document where we're storing planetary data. In that XML document, we have three <planet> elements as children of the main <planets> element:

<planets>

  <planet language="English">
    <name>Mercury</name>
    <mass units="(Earth = 1)">.0553</mass>
    <day units="days">58.65</day>
    .
    .
    .
  <planet language="English">
    <name>Venus</name>
    <mass units="(Earth = 1)">.815</mass>
    <day units="days">116.75</day>
    .
    .
    .
  <planet language="English">
    <name>Earth</name>
    <mass units="(Earth = 1)">1</mass>
    <day units="days">1</day>
    .
    .
    .

To select all three <planet> elements, you can use the absolute XPath expression /planets/planet, which starts at the XML document's root node, finds the <planets> element, and then matches the three <planet> child elements. That's fine if you know exactly where in the XML document the elements you want are and so can specify a direct path to them.

But you can also use //planet to select all three <planet> elements, because //planet will find the <planet> elements by checking the root node and all descendants for <planet> elements. That's the power of //—when you want to search for nodes that may be anywhere in a document, use //.

How about some examples of location paths using abbreviated syntax? Here are a number of examples:

  • planet—Returns the <planet> element children of the context node.

  • *—Returns all element children of the context node.

  • text()—Returns all text node children of the context node.

  • @units—Returns the units attribute of the context node.

  • @*—Returns all the attributes of the context node.

  • planet[3]—Returns the third <planet> child of the context node.

  • planet[first()]—Returns the first <planet> child of the context node.

  • */planet—Returns all <planet> grandchildren of the context node.

  • /planets/planet[3]/name[2]—Returns the second <name> element of the third <planet> element of the <planets> element.

  • //planet—Returns all the <planet> descendants of the root node.

  • planets//planet—Returns the <planet> element descendants of the <planets> element children of the context node.

  • //planet/name—Returns all the <name> elements that have a <planet> parent.

  • .—Returns the context node itself.

  • .//planet—Returns the <planet> element descendants of the context node.

  • ..—Returns the parent of the context node.

  • ../@units—Returns the units attribute of the parent of the context node.

  • planet[name]—Returns the <planet> children of the context node that have <name> children.

  • planet[name="Venus"]—Returns the <planet> children of the context node that have <name> children with text equal to "Venus".

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020