Home > Articles > Microsoft > Other Microsoft

This chapter is from the book

Understanding XPath

To pass the exam, you should also have a basic knowledge of XPath. You can think of XPath as being a query language, conceptually similar to SQL. Just as SQL allows you to select a set of information from a table or group of tables, XPath allows you to select a set of nodes from the DOM representation of an XML document. By writing an appropriate XPath expression, you can select particular elements or attributes within an XML document.

The XPath Language

XPath starts with the notion of current context. The current context defines the set of nodes that will be inspected by an XPath query. In general, there are four choices to specify the current context for an XPath query:

  • ./—Uses the current node as the current context

  • /—Uses the root of the XML document as the current context

  • .//—Uses the entire XML hierarchy starting with the current node as the current context

  • //—Uses the entire XML document as the current context

To identify a set of elements using XPath, you use the path down the tree structure to those elements, separating tags by forward slashes. For example, this XPath expression selects all the Author elements in the Books.xml file:

/Books/Book/Author

You can also select all the Author elements without worrying about the full path to get to them by using this expression:

//Author

You can use an asterisk (*) as a wildcard at any level of the tree. So, for example, the following expression selects all the Author nodes that are grandchildren of the Books node:

/Books/*/Author

XPath expressions select a set of elements, not a single element. Of course, the set might only have a single member, or no members at all. In the context of the XmlDocument object, an XPath expression can be used to select a set of XmlNode objects to operate on later.

To identify a set of attributes, you trace the path down the tree to the attributes, just as you do with elements. The only difference is that attribute names must be prefixed with the @ character. For example, this XPath expression selects all the Pages attributes from Book elements in the Books.xml file:

//Book/@Pages

Of course, in the Books.xml file, only Book elements have a Pages attribute. So in this particular context, the following XPath expression is equivalent to the previous one:

//@Pages

You can select multiple attributes with the @* operator. To select all attributes of Book elements anywhere in the XML, use this expression:

//Book/@*

XPath also offers a predicate language to allow you to specify smaller groups of nodes or even individual nodes in the XML tree. You might think of this as a filtering capability similar to a SQL WHERE clause. One thing you can do is specify the exact value of the node you'd like to work with. To find all Publisher nodes with the value "Que," you could use the following XPath expression:

/Books/Book/Publisher[.="Que"]

Here, the dot operator stands for the current node. Alternatively, you can find all books published by Que:

/Books/Book[./Publisher="Que"]

Note that there is no forward slash between an element and a filtering expression in XPath.

Of course, you can filter on attributes as well as elements. You can also use operators and Boolean expressions within filtering specifications. For example, you might want to find books that have 1,000 or more pages:

/Books/Book[./@Pages>=1000]

Because the current node is the default context, you can simplify this expression a little bit:

/Books/Book[@Pages>=1000]

XPath also supports a selection of filtering functions. For example, to find books whose title starts with A, you could use this XPath expression:

/Books/Book[starts-with(Title,"A")]

Table 3.5 lists some additional XPath functions.

Table 3.5 Selected XPath Functions

Function

Description

concat

Concatenates strings

contains

Determines whether one string contains another

count

Counts the number of nodes in an expression

last

The last element in a collection

normalize-space

Removes whitespace from a string

not

Negates its argument

number

Converts its argument to a number

position

The ordinal of a node within its parent

starts-with

Determines whether one string starts with another

string-length

Returns the number of characters in a string

substring

Returns a substring from a string


Square brackets are also used to indicate indexing. Collections are indexed starting at one. To return the first Book node, you'd use this expression:

/Books/Book[1]

To return the first author in the XML file (knowing that authors are children of books in this file), regardless of the book, you'd use this expression:

(/Books/Book/Author)[1]

The parentheses are necessary because the square brackets have a higher operator precedence than the path operators. Without the brackets, the expression would return the first author of every book in the file. There's also the last() function, which you can use to return the last element in a collection, without needing to know how many elements are in the collection:

/Books/Book[last()]

Another useful operator is the vertical bar, which is used to form the union of two sets of nodes. This expression returns all the authors for books published by Addison-Wesley or Que:

/Books/Book[./Publisher="Addison-Wesley"]/Author|
 /Books/Book[./Publisher="Que"]/Author

One way to see XPath in action is to use the SelectNodes method of the XmlDocument object. Here's how you could load the Books.xml file and select nodes matching a particular XPath expression:

' Load the Books.xml file
Dim xtr As XmlTextReader = _
 New XmlTextReader("Books.xml")
xtr.WhitespaceHandling = WhitespaceHandling.None
Dim xd As XmlDocument = _
 New XmlDocument()
xd.Load(xtr)
' Retrieve nodes to match the expression
Dim xnl As XmlNodeList = _
 xd.DocumentElement.SelectNodes("//Books/Book/Title")

The SelectNodes method of the XmlDocument takes an XPath expression and evaluates that expression over the document. The resulting nodes are returned in an XmlNodeList object, which is just a collection of XML nodes.

Using the XPathNavigator Class

You've seen how you can use the XmlReader class to move through an XML document. But the XmlReader allows only forward-only, read-only access to the document. The System.Xml.XPath namespace contains another set of navigation classes. In particular, the XPathNavigator class provides you with read-only, random access to XML documents.

You can perform two distinct tasks with an XPathNavigator object:

  • Selecting a set of nodes with an XPath expression

  • Navigating the DOM representation of the XML document

Selecting Nodes with XPath

To use the XPathNavigator class, you should start with an XmlDocument, XmlDataDocument, or XPathDocument object. In particular, if you're mainly interested in XPath operations, you should use the XPathDocument class. The XPathDocument class provides a representation of the structure of an XML document that is optimized for query operations. You can construct an XPathDocument object from a URI (including a local filename), a stream, or a reader containing XML.

The XPathDocument object has a single method of interest, CreateNavigator (you'll also find this method on the XmlDocument and XmlDataDocument objects). This method returns an XPathNavigator object that can perform operations with the XML document represented by the XPathDocument object. Table 3.6 lists the important members of the XPathNavigator object.

Table 3.6 Important Members of the XPathNavigator Class

Member

Type

Description

Clone

Method

Creates a duplicate of this object with the current state

ComparePosition

Method

Compares two XPathNavigator objects to determine whether they have the same current node

Compile

Method

Compiles an XPath expression for faster execution

Evaluate

Method

Evaluates an XPath expression

HasAttributes

Property

Indicates whether the current node has any attributes

HasChildren

Property

Indicates whether the current node has any children

IsEmptyElement

Property

Indicates whether the current node is an empty element

Matches

Method

Determines whether the current node matches an XSLT (Extensible Stylesheet Language Transform) pattern

MoveToFirst

Method

Moves to the first sibling of the current node

MoveToFirstAttribute

Method

Moves to the first attribute of the current node

MoveToFirstChild

Method

Moves to the first child of the current node

MoveToNext

Method

Moves to the next sibling of the current node

MoveToNextAttribute

Method

Moves to the next attribute of the current node

MoveToParent

Method

Moves to the parent of the current node

MoveToPrevious

Method

Moves to the previous sibling of the current node

MoveToRoot

Method

Moves to the root node of the DOM

Name

Property

The qualified name of the current node

Select

Method

Selects a set of nodes using an XPath expression

Value

Property

The value of the current node


CAUTION

Unlike the XmlReader class, the XPathNavigator class implements methods such as MovePrevious and MoveParent that can move backward in the DOM. The XPathNavigator class provides random access to the entire XML document.

Like the XmlReader class, the XPathNavigator class maintains a pointer to a current node in the DOM at all times. But the XPathNavigator brings additional capabilities to working with the DOM. For example, you can use this class to execute an XPath query by calling its Select method:

' Load the Books.xml file
Dim xpd As XPathDocument = _
 New XPathDocument("Books.xml")
' Get the associated navigator
Dim xpn As XPathNavigator = _
 xpd.CreateNavigator()
' Retrieve nodes to match the expression
Dim xpni As XPathNodeIterator = _
 xpn.Select("//Books/Book/Title")
' And dump the results
lbNodes.Items.Clear()
While xpni.MoveNext
 lbNodes.Items.Add( _
  xpni.Current.NodeType.ToString _
  & ": " & xpni.Current.Name & " = " & _
  xpni.Current.Value)
End While

The Select method of the XPathNavigator class returns an XPathNodeIterator object, which lets you visit each member of the selected set of nodes in turn.

Navigating Nodes with XPath

You can also use the XPathNavigator object to move around in the DOM. To see how this works, try following these steps:

  1. Open a Visual Basic .NET Windows Application project and add a new form to the project.

  2. Add four Button controls (btnParent, btnPrevious, btnNext, and btnChild) and a ListBox control named lbNodes to the form.

  3. Double-click the Button control to open the form's module. Add this line of code at the top of the module:

  4. Imports System.Xml.XPath
  5. Add code to load an XML document when you load the form:

  6. Dim xpd As XPathDocument
    Dim xpn As XPathNavigator
    
    Private Sub StepByStep2_9_Load( _
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles MyBase.Load
     ' Load the Books.xml file
     xpd = New XPathDocument("Books.xml")
     ' Get the associated navigator
     xpn = xpd.CreateNavigator()
     xpn.MoveToRoot()
     ListNode()
    End Sub
    
    Private Sub ListNode()
     ' Dump the current node to the listbox
     lbNodes.Items.Add( _
      xpn.NodeType.ToString _
      & ": " & xpn.Name & " = " & _
      xpn.Value)
    End Sub
  7. Add code to handle events from the Button controls:

  8. Private Sub btnParent_Click( _
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnParent.Click
     ' Move to the parent of the current node
     If xpn.MoveToParent() Then
      ListNode()
     Else
      lbNodes.Items.Add("No parent node")
     End If
    End Sub
    
    Private Sub btnPrevious_Click( _
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) _
     Handles btnPrevious.Click
     ' Move to the previous sibling of the current node
     If xpn.MoveToPrevious() Then
      ListNode()
     Else
      lbNodes.Items.Add("No previous node")
     End If
    End Sub
    
    Private Sub btnNext_Click( _
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnNext.Click
     ' Move to the next sibling of the current node
     If xpn.MoveToNext() Then
      ListNode()
     Else
      lbNodes.Items.Add("No next node")
     End If
    End Sub
    
    Private Sub btnChild_Click( _
     ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles btnChild.Click
     ' Move to the first child of the current node
     If xpn.MoveToFirstChild() Then
      ListNode()
     Else
      lbNodes.Items.Add("No child node")
     End If
    End Sub
  9. Set the form as the startup form for the project.

  10. Run the project and then experiment with the buttons. You'll find that you can move around in the DOM, as shown in Figure 3.2.

  11. Figure 3.2Figure 3.2 Exploring an XML document with the XPathNavigator.

The XPathNavigator class does not throw an error if you try to move to a nonexistent node. Instead, it returns False from the method call, and the current node remains unchanged.

Pearson IT Certification Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Pearson IT Certification 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 Pearson IT Certification 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 Adobe Press. 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.pearsonitcertification.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