Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

Module 2: Selecting and Navigating Nodes Using XPath

Contents Overview Introducing XPath Accessing Node Information Lab 2.1: Accessing Node Information 1 2 9 17

Navigating a Document Using Location Paths21 Using Operators and Functions in Location Paths 35 Lab 2.2: Selecting and Navigating Nodes Using XPath Review 45 53

Information in this document is subject to change without notice. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Complying with all applicable copyright laws is the responsibility of the user. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation. If, however, your only means of access is electronic, permission to print one copy is hereby granted. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2001 Microsoft Corporation. All rights reserved. Microsoft, ActiveX, BackOffice, Jscript, MSDN, MS-DOS, PowerPoint, SQL Server, Visual Basic, Visual C++, Visual InterDev, Windows, Windows Media Player, and Windows NT are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. Other product and company names mentioned herein may be the trademarks of their respective owners.

Module 2: Selecting and Navigating Nodes Using XPath

Overview
Introducing XPath Accessing Node Information Navigating a Document Using Location Paths Using Operators and Functions in Location Paths

In this module, you will learn about the XML Path Language (XPath), a language that provides a standard syntax for identifying nodes in an XML document. This module describes the XPath language, and shows you how to use XPath to access nodes in an XML document. After completing this module, you will be able to: Describe how to use XPath in XML-based applications. Describe the XPath data model. Use XPath expressions within the Document Object Model (DOM) methods selectNodes() and selectSingleNode(). Write XPath location paths to select specific nodes from an XML document. Write conditional expressions in XPath. Use XPath operators and functions to write more sophisticated expressions.

Module 2: Selecting and Navigating Nodes Using XPath

Introducing XPath
What Is XPath? Why Use XPath? Selecting XML Nodes in the DOM Using XPath Demonstration: Accessing XML Nodes Using XPath

XPath is a standard language for locating nodes and data in an XML document. In this section, you will learn how various technologies make use of XPath. You will also learn how to combine XPath with Document Object Model (DOM) methods to access data from an XML document.

Module 2: Selecting and Navigating Nodes Using XPath

What Is XPath?

XPath maps an XML document to a tree of nodes Use XPath expressions to identify, select, and manipulate nodes in an XML hierarchy Use location paths to select nodes XPath operators and functions

When working with XML, you typically need to select and manipulate nodes of an XML document programmatically. To meet this need, the World Wide Web Consortium (W3C) has defined XPath as a standard language for selecting individual nodes or groups of nodes in an XML document.

Mapping XML Documents


The XPath data model maps an XML document to a tree of standard node types: root, element, attribute, text, comment, processing-instruction, and namespace nodes. The mapping rules ensure that a given XML document will always map to the same XPath tree structure.

Identifying, Selecting, and Manipulating Nodes


An XML document is mapped to a tree of nodes when the document is loaded into an XML parser. The individual nodes and groups of nodes in the tree can then be identified, selected, and manipulated. To accomplish this, you use XPath expressions, which recognize the standard node types defined in the XPath data mode. A location path is a special case of XPath expression used specifically to select individual nodes or groups of nodes. A location path can select a node by type, value, or position. To increase the range of use of XPath expressions, you can include operators and functions.

Module 2: Selecting and Navigating Nodes Using XPath

Why Use XPath?


In XSLT style sheets, for pattern matching In the DOM, to select nodes programmatically

XPath XPath XML XML

XML XML XML XML In XPointer, to link documents together In SQL Server 2000, to address result-set nodes

You can use XPath expressions in conjunction with other key XML technologies whenever there is a need to identify, select, or manipulate nodes.

Using XPath to Select Nodes in XSLT Style Sheets


Extensible Stylesheet Language Transformations (XSLT) style sheets define a series of template rules that select and transform nodes in a source XML document. Each template rule matches specific nodes in the XML document and specifies how these nodes should be transformed in the output document. When you define an XSLT template, you must: 1. Use an XSLT <xsl:template> element to establish an XSLT template rule. 2. Specify an XPath expression as the match attribute of the <xsl:template> element, to select matching nodes in the XML document. 3. Define how the selected nodes will be transformed. Example In this example, an XSLT template rule uses an XPath expression to select <product> elements in an XML document. The <xsl:value-of> element gets the value of each selected <product> element.
<xsl:template match="product"> <xsl:value-of select="." /> </xsl:template>

Using XPath in the Document Object Model (DOM)


The DOM is a programming interface for XML. The Microsoft DOM parser allows XPath expressions to be used to select nodes. You will learn how to select nodes by using DOM methods and XPath expressions later in this module.

Module 2: Selecting and Navigating Nodes Using XPath

Using XPath in XPointer


You can use XPath in conjunction with the XML Pointer Language (XPointer) to create a link in one document to nodes in another document. These nodes are similar to hyperlinks in the Hypertext Markup Language (HTML). You can link to any part of the document by specifying the required node in an XPath expression. The XPointer approach offers two main advantages over the hyperlinks approach: XPointer is more flexible and extensible. The author of a document doesnt have to anticipate which bookmarks might be useful in a document. XPointer uses XPath expressions, which make it possible to define conditional links between documents. An XML document can link to different content depending on the result of an XPath expression. XPointer has not yet been finalized as a standard, and is subject to change before it achieves Recommendation status. Commercial XML parsers do not currently support XPointer, including any version of the Microsoft parser. This course does not further address details of the XPointer specification. For more information, see the draft specification at http://www.w3.org/XML/Linking

Using XPath in SQL Server 2000


Microsoft SQL Server 2000 can return query results formatted as an XML document. You can query SQL Server 2000 by creating an annotated schema that functions like an XML view on the underlying relational tables. You then use an XPath expression to select nodes from this XML view and generate a new XML document.

Module 2: Selecting and Navigating Nodes Using XPath

Selecting XML Nodes in the DOM Using XPath


DOM is a programmers interface to XML documents Defines methods to add, remove, or reorganize content Microsofts XML parsers define two methods for selecting nodes by using an XPath expression: selectSingleNode selects the first matching node selectNodes selects all matching nodes
Set Set document document = = CreateObject("MSXML2.DOMDocument.3.0") CreateObject("MSXML2.DOMDocument.3.0") document.setProperty document.setProperty "SelectionLanguage", "SelectionLanguage", "XPath" "XPath" Document.async Document.async = = False False document.load("MyCatalog.xml") document.load("MyCatalog.xml") Set Set catalogNode catalogNode = = document.selectSingleNode("//catalog") document.selectSingleNode("//catalog") Set Set productNodes productNodes = = catalog.selectNodes("product") catalog.selectNodes("product")

The DOM makes an XML document available as a tree of nodes. One of the common uses of the DOM is to locate specific nodes or collections of nodes in an XML document.

Using XPath Expressions and the DOM to Select Nodes


The Microsoft XML Parser (MSXML3) defines two useful DOM methods for taking an XPath expression and selecting matching nodes in the document: selectSingleNode returns the first matching node. selectNodes returns all matching nodes.

Selecting Nodes
When you select nodes by using the selectSingleNode or selectNodes methods, you must: 1. Establish a reference to the DOM by using the MSXML2.DOMDocument.3.0 version of the parser. 2. Use the setProperty function to tell the parser to use XPath expressions to select nodes in the XML document. If you do not set this property, the parser uses a non-standard pattern-matching syntax from an earlier version of the parser. 3. Set the documents async property to False. This ensures that the document is loaded synchronously. 4. Specify an XPath expression or location path as a parameter of the selectSingleNode or selectNodes method.

Module 2: Selecting and Navigating Nodes Using XPath

Example

In this example, DOM methods use XPath expressions to pull a specific set of nodes from an XML document. The code selects the first <catalog> element in the document, and then selects all of its <product> child elements.
Set document = CreateObject("MSXML2.DOMDocument.3.0") document.setProperty "SelectionLanguage", "XPath" document.async = False document.load("MyCatalog.xml") Set catalogNode = document.selectSingleNode("//catalog") Set productNodes = catalog.selectNodes("product")

Module 2: Selecting and Navigating Nodes Using XPath

Demonstration: Accessing XML Nodes Using XPath

In this demonstration, you will see how XPath expressions can be used to identify nodes in an XML document. The demonstration uses an HTML form that loads an XML document into an MSXML parser at startup. The HTML document has the following visual components: A <DIV> section, where the XML document is displayed. A drop-down list box, where the user can choose an XPath expression. A button that calls selectSingleNode against the XML document, using the XPath expression chosen by the user. A button that calls selectNodes against the XML document, using the XPath expression chosen by the user. The following XPath expressions may be used.
XPath Expression /catalog/product /catalog/product/price Description Returns the product child elements of catalog. Returns the price child elements of product elements, which themselves are children of catalog. Returns the code attribute of product elements, which themselves are children of catalog. Returns the first product in the catalog. Returns the last product in the catalog.

/catalog/product/@code

/catalog/product[1] /catalog/product[last()]

The results of the selectSingleNode and selectNodes methods are displayed in message boxes for clarity.

Module 2: Selecting and Navigating Nodes Using XPath

Accessing Node Information


XPath Tree Structure and Node Types Obtaining Information About Each Node Using Nodes in the DOM

In this section, you will learn how XML documents are modeled as a tree in XPath. You will also learn about the properties contained in each node, and see how to access this information by using the DOM.

10

Module 2: Selecting and Navigating Nodes Using XPath

XPath Tree Structure and Node Types


XML documents map to an XPath tree An XPath tree contains nodes arranged in a hierarchy
<catalog <catalog xmlns="urn:litwareinc"> xmlns="urn:litwareinc"> <?proc <?proc instr?> instr?> <!--comment--> <!--comment--> <product <product code="123"> code="123"> Blue Blue jeans jeans </product> </product> </catalog> </catalog>
"urn:litwareinc" "urn:litwareinc" "123" "123" "Blue "Blue jeans" jeans" Root Root

<catalog> <catalog> "urn:litware" "urn:litware" "proc-instr" "proc-instr" "comment "comment "" <product> <product>

XPath treats an XML document as a tree of nodes. The ordering of nodes corresponds to the ordering of content in the XML document. There are seven node types in XPath: Root nodes Element nodes Attribute nodes Text nodes Namespace nodes Processing instruction nodes Comment nodes

Root Nodes
Every XML document has a single root node, which lies at the top of the XPath tree. The root node in XPath has the same role as the document node in the DOM.

Element Nodes
A separate element node in the XPath tree represents every element in an XML document. A well-formed XML document has a single document element, and any number of child elements and descendant elements.

Module 2: Selecting and Navigating Nodes Using XPath

11

Attribute Nodes
Elements can have zero or more attributes. Note the following points about attribute nodes: Every attribute is represented by a separate attribute node. If multiple elements have the same attribute name and value, each attribute is represented by a different attribute node. Attribute nodes are not considered to be children of the elements in which they are defined. If an attribute is not explicitly defined but has a default value specified in a Document Type Definition (DTD), it still appears in the XPath tree. Namespace declarations look like attributes, but they are represented by namespace nodes in the XPath tree.

Text Nodes
A text node is a sequence of consecutive characters defined in an elements content. Any adjacent text is normalized to form a single text node. A text node always contains at least one character.

Namespace Nodes
Each element has a set of namespace nodes that describe all of the namespace definitions within scope for that element. This includes namespaces defined in ancestor elements, plus the default namespace (if one has been defined). Namespace nodes are not shared. If an element defines a namespace that also applies to its child elements, then each child element will have a distinct namespace node.

Processing Instruction Nodes


Processing instructions provide information to help the parser process the XML document. Processing instructions use the <? ?> syntax. XML declarations also use the syntax <?xml version="1.0"?>. However, XML declarations are not considered to be processing instructions. XML declarations are not represented in an XPath tree.

Comment Nodes
Comment nodes contain the comment text enclosed between the delimiters <!-and -->.

12

Module 2: Selecting and Navigating Nodes Using XPath

Obtaining Information About Each Node


Each node has properties that describe the node Node types Root Element Attribute Text Namespace Processing instruction Comment Properties Value Name Parent node Child nodes

Each node in an XPath tree has four properties. These properties describe the value, name, and location of the node. The properties are as follows: The value of the node. This property is a Unicode string. The name of the node. This property gives the nodes name, plus its namespace Uniform Resource Identifier (URI) if defined. The parent node. All XPath nodes have a single parent node, except for the root node, which does not have a parent node. The list of child nodes. This property returns a node-set specifying the direct children of a node.

Root Node Properties


This table describes the relevant properties for the root node.
Property Value Child nodes Contains Concatenated string values for all text nodes in the document. Contains the document element, and any processing instructions and comments before the start or after the end of the document element.

Module 2: Selecting and Navigating Nodes Using XPath

13

Element Node Properties


This table describes the relevant properties for element nodes.
Property Value Name Parent node Contains Concatenated string values for all descendant text nodes. The name of the element. If the element is in a namespace, the namespace URI is included in the elements name. A reference to the elements parent node. This will be another element node, except in the case of the document element. The parent of the document element is the root node. Elements, text nodes, processing instructions, and comments defined as children of the element.

Child nodes

Attribute Node Properties


This table describes the relevant properties for attribute nodes.
Property Value Name Contains The value of the attribute. The name of the attribute. If a namespace prefix appears explicitly on the attribute, the namespace URI is included in the attributes name. Otherwise, the namespace URI is not included in the attributes name. The containing element node. Note that although attributes have elements as parents, attributes are not considered children of elements.

Parent node

Text Node Properties


This table describes the relevant properties for text nodes.
Property Value Parent node Contains The character data itself. The text node always contains at least one character. The containing element of the text node.

Namespace Node Properties


Consider the namespace xmlns:lit="urn:litwareinc". The following table describes the relevant properties for namespace nodes.
Property Value Name Parent node Contains The namespace URI, urn:litwareinc The namespace prefix, lit The containing element of the namespace node

14

Module 2: Selecting and Navigating Nodes Using XPath

Processing Instruction Node Properties


This table describes the relevant properties for processing instruction nodes.
Property Value Name Parent node Contains The part of the processing instruction following the target, including any whitespace. The target of the processing instruction. For example, in <?xmlstylesheet ?>, the name is xml-stylesheet. The containing element of the processing instruction node, or the root node if declared outside the document element.

Comment Node Properties


This table describes the relevant properties for comment nodes.
Property Value Parent node Contains The contents of the comment The containing element of the comment node, or the root node if declared outside the document element

Module 2: Selecting and Navigating Nodes Using XPath

15

Using Nodes in the DOM


You can access node information using DOM properties
Set Set node node = = document.selectSingleNode("//product") document.selectSingleNode("//product") MsgBox MsgBox MsgBox MsgBox MsgBox MsgBox MsgBox MsgBox "Text & "Text value: value: " " & node.text node.text "Node base-name: " & node.baseName "Node base-name: " & node.baseName "Namespace "Namespace prefix: prefix: " "& & node.prefix node.prefix "Namespace & "Namespace URI: URI: " " & node.namespaceURI node.namespaceURI

Set Set parent parent = = node.parentNode node.parentNode MsgBox MsgBox "Name "Name of of parent: parent: " "& & parent.baseName parent.baseName Set Set For For children children = = Each Each child child node.childNodes node.childNodes In In children children

The DOM allows you to specify an XPath expression in the selectSingleNode and selectNodes methods, to select nodes in an XML document. Once you have a node, you can use DOM properties to find out more information about the node. The following table describes the commonly used DOM properties for retrieving node information.
DOM property text baseName prefix namespaceURI parentNode childNodes Description Gets the text value of the node. For example, the text property for an attribute is the value of the attribute. Gets the name of the node, without any namespace information. Gets the namespace prefix for an element or attribute. Gets the namespace URI for an element or attribute. Gets the parent node. Gets a NodeList containing all of the children of the node.

16

Module 2: Selecting and Navigating Nodes Using XPath

Example

In this example, the DOM selectSingleNode method uses an XPath expression to return the first <product> element in an XML document. Message boxes use the DOM to display useful information about the element.
Set document = CreateObject("MSXML2.DOMDocument.3.0") document.setProperty "SelectionLanguage", "XPath" document.async = False document.load("MyCatalog.xml") Set node = document.selectSingleNode("//product") MsgBox MsgBox MsgBox MsgBox "Text value: " & "Base name: " & "Namespace prefix: " & "Namespace URI: " & node.text node.baseName node.prefix node.namespaceURI

Set parent = node.parentNode


MsgBox "Name of parent: " & parent.baseName Set children = node.childNodes For Each child In Children MsgBox "Child node name: " & child.baseName MsgBox "Child node value: " & child.text Next

Module 2: Selecting and Navigating Nodes Using XPath

17

Lab 2.1: Accessing Node Information

Objectives
After completing this lab, you will be able to: Use XPath expressions in the XML Document Object Model (DOM) to locate content in an XML document. Obtain detailed information about the nodes in an XML document.

Prerequisites
Before working on this lab, you must have: Familiarity with Microsoft Visual Basic Scripting Edition (VBScript). A working knowledge of the DOM.

Scenario
In this lab, you will add VBScript to an HTML page in order to load an XML document and navigate its contents by using XPath expressions. You will use the DOM methods SelectSingleNode() and SelectNodes() to display the results.

Starter and Solution Files


There are starter files and solution files for this lab. Starter files are in the folder <install folder>\Labs\Lab02.1\Starter. Solution files are in the folder <install folder>\Labs\Lab02.1\Solution.

Estimated time to complete this lab: 45 minutes

18

Module 2: Selecting and Navigating Nodes Using XPath

Exercise 1 Selecting Single Nodes Using XPath and the DOM


In this exercise, you will load an XML document into a Microsoft XML (MSXML) parser. You will use the selectSingleNode() method to select a single node from the document. You will then obtain the name, value, and parent information for this node. To load an XML document into an MSXML parser 1. Open NodeProperties.htm with Microsoft Notepad from the folder <install folder>\Labs\Lab02.1\Starter. Locate the script function Window_OnLoad(). 2. Use the CreateObject() function to create an instance of the MSXML2.DOMDocument.3.0 object. Store a reference to this variable in the global variable named document. 3. Use the setProperty() method of the document to set the SelectionLanguage property to XPath. 4. Set the async property of the document object equal to False. 5. Load the XML document Catalog.xml into the document object. 6. Display the XML by assigning divXMLDisplay.innerText = document.xml. 7. Open http://localhost/1913/Labs/Lab02.1/Starter/NodeProperties.htm with Microsoft Internet Explorer. Examine the structure and content of the XML document. To obtain the name and value of a node 1. Open NodeProperties.htm with Notepad and find the script function btn1_OnClick(). This function is called when the btn1 button is clicked in the HTML document. 2. Use the selectSingleNode() method of the document object to get the first <product> element node. 3. Use the baseName and text DOM properties to get the name and value of this node. 4. Use the MsgBox function to display these details. 5. Open http://localhost/1913/Labs/Lab02.1/Starter/NodeProperties.htm with Internet Explorer. Click button 1. The message box displays the name and value of the <product> element:
product Blue Jeans 35.99

Module 2: Selecting and Navigating Nodes Using XPath

19

To obtain information about the parent node 1. Open NodeProperties.htm with Notepad and find the script function btn2_OnClick(). This function is called when the btn2 button is clicked in the HTML document. 2. Use the selectSingleNode() method of the document object to get the first <product> element node. 3. Use the parentNode DOM property to get the parent node. 4. Use the MsgBox function to display the name and value of the parent node. 5. Open http://localhost/1913/Labs/Lab02.1/Starter/NodeProperties.htm with Internet Explorer. Click button 2. The message box displays the name and value of the parent node:
catalog Blue Jeans 35.99 Plaid shirt 22.95 Leather Jacket 185.00

20

Module 2: Selecting and Navigating Nodes Using XPath

Exercise 2 Selecting Multiple Nodes Using XPath and DOM


In this exercise, you will display details for all of the child nodes of the first <product> element. You will then use the selectNodes() method to obtain details for all <product> elements in the XML document. To obtain information about child nodes 1. In NodeProperties.htm, locate the script function btn3_OnClick(). 2. Use the selectSingleNode() method of the document object to get the first <product> element node. 3. Use the childNodes DOM property to get a list of its child nodes. 4. Create a loop to step through the list of child nodes. Use the MsgBox function to display the name and value of each child node. 5. Open http://localhost/1913/Labs/Lab02.1/Starter/NodeProperties.htm with Internet Explorer. Click button 3. Message boxes display the following information:
Child node name: description Child node value: Blue Jeans Child node name: price Child node value: 35.99

To obtain information about all <product> elements 1. In NodeProperties.htm, locate the script function btn4_OnClick(). 2. Use the selectNodes() method of the document object to select all of the <product> elements in the document. 3. Create a loop to step through the list of <product> elements. Use the MsgBox function to display the name and value of each <product> element. 4. Open http://localhost/1913/Labs/Lab02.1/Starter/NodeProperties.htm with Internet Explorer. Click button 4. Three message boxes appear, one after the other, displaying the following information:
product Blue Jeans 35.99 product Plaid shirt 22.95 product Leather jacket 185.00

Module 2: Selecting and Navigating Nodes Using XPath

21

Navigating a Document Using Location Paths


What Is a Location Path? Constructing Location Paths Understanding Axes Using Axes in XPath Using Node Tests Using Predicates to Filter Nodes Demonstration: Navigating a Document Using Location Paths

In this section, you will learn how to use a special type of XPath expression known as a location path, which is used to select specific nodes in an XML document. Location paths can be used to select any type of node in an XML document, at any relative or absolute position.

22

Module 2: Selecting and Navigating Nodes Using XPath

What Is a Location Path?


A location path is an XPath expression for locating nodes in an XML document Locates nodes conveniently and efficiently Location paths may be relative or absolute Relative location paths Define a location relative to the current node Absolute location paths Begin with a / character Define a location relative to the root node

A location path is an XPath expression that contains a sequence of teststhat locate specific nodes in an XML document. A location path selects nodes in a hierarchical XML document, similarly to the way a file path selects files in a hierarchical file system. There are two types of location paths: Relative location paths Absolute location paths

Defining a Relative Location Path


A relative location path defines a path through an XML document, relative to the current node in the document. Relative location paths are useful when you have already located a particular node in an XML document, and you want to find a nearby node such as a child element or the parent node. Example This relative location path selects child elements named <product>, relative to the current position in the XML document. The location path then selects the <description> child elements of these <product> elements:
product/description

Defining an Absolute Location Path


An absolute location path defines a path through an XML document, starting at the root node in the document. Absolute location paths begin with a forward slash character ( / ). Absolute location paths are useful when your current location in the document is not important, and you want to perform a fresh search from the root of the document.

Module 2: Selecting and Navigating Nodes Using XPath

23

Example

This example shows an absolute location path. Note that the location path starts with a forward slash character. The location path selects the <catalog> element, relative to the root node. The <product> child elements are then retrieved:
/catalog/product

24

Module 2: Selecting and Navigating Nodes Using XPath

Constructing Location Paths


A location path is made up of location steps Relative path Absolute path
product/price product/price /catalog/product/price /catalog/product/price

Location steps are evaluated from left to right Specify a path through the XML document Each location step is made up of three parts
axis::node-test[predicate] axis::node-test[predicate]

Example location step


child::product[price child::product[price > > 35.95][3] 35.95][3]

A location path is an expression that selects nodes in an XML document. The result of a location path is called a node-set. A node-set may contain many nodes, a single node, or no nodes at all. A location path defines a search path through an XML document. A relative location path starts at the context node. An absolute location path starts at the root node.

Analyzing Location Paths


A location path is composed of one or more location steps, each separated by a forward slash character. Each location step yields a node-set. Location steps are strung together from left to right, with each location step feeding its node-set into the next location step to yield a more specific or refined node-set. A relative location path may be defined as follows:
location-step/location-step/location-step etc.

To define an absolute location path, place a forward slash character at the beginning of the location path, as follows:
/location-step/location-step/location-step etc.

Module 2: Selecting and Navigating Nodes Using XPath

25

Examples

In this example, a relative location path is made up of two location steps. Starting at the context node, the first location step returns a node-set composed of <product> elements. The second location step selects any <price> child elements.
product/price

In the next example, an absolute location path is constructed of three location steps. Starting at the document root, the first location step selects the <catalog> document element. The next location step selects <product> child elements of the <catalog> element. The last location step selects the <price> child elements of the <product> elements.
/catalog/product/price

Specifying Search Criteria in a Location Step


Each location step specifies an axis, a node test, and one or more optional predicates, as described in this section. The syntax follows this pattern:
axis::node-test[predicate]

Axis The axis defines what type of search you wish to perform. For example, the child axis selects child elements, and the attribute axis selects attributes. The full set of axes is described in the following section. Node test The node test specifies the names of the nodes or the type of nodes you wish to select. Typically, this is the name of an element or attribute. Predicate A predicate is a Boolean expression that filters the node-set obtained in the node test. A location step can have zero or more predicates. Each predicate expression is enclosed in brackets ([]) and filters nodes that match the specified criterion. Example In this example, a relative location path applies a product node test to a search through the child axis. Two predicates filter the node-set. The first predicate keeps only those <product> elements with a <price> greater than 35.95. The next predicate keeps only the third such <product> element.
child::product[price > 35.95][3]

26

Module 2: Selecting and Navigating Nodes Using XPath

Understanding Axes
<catalog> <product code="123"> <price>35.99</price> </product> <product code="456"> <price>22.95</price> <discount> <amount>25</amount> </discount> </product> <product code="789"> <price>185.00</price> </product> </catalog>
<product> code=123 <price> 35.99 code=456

self child parent attribute descendant descendant-or-self ancestor ancestor-or-self


Root node <catalog> <product> <price> 22.95

following following-sibling preceding preceding-sibling

<product> <discount> <amount> 25 code=789 <price> 185.00

XPath defines 13 different axes described in the following table. These axes enable you to search an XML document in any way you wish.
XPath axis self child parent attribute descendant descendant-or-self ancestor ancestor-or-self following following-sibling preceding Description Contains only the context node. Contains the child nodes of the context node. Contains the parent node of the context node. Contains attributes of the context node. Contains the child nodes of the context node, plus their child nodes, and so on. Contains the descendants of the context node, plus the context node itself. Contains the parent of the context node, plus its parent, and so on, up to (and including) the root node. Contains the ancestors of the context node, plus the context node itself. Contains all nodes (except descendant nodes, attribute nodes, and namespace nodes) that appear after the closing tag of the context node. Contains all of the following siblings of the context node. If the context node is an attribute node or a namespace node, the node-set is empty. Contains all nodes (except ancestor nodes, attribute nodes, and namespace nodes) that appear before the opening tag of the context node. Contains all of the preceding siblings of the context node. If the context node is an attribute node or a namespace node, the node-set is empty. Contains the namespace nodes of the context node.

preceding-sibling

namespace

Module 2: Selecting and Navigating Nodes Using XPath

27

Using Axes in XPath


Commonly used XPath axes may have unabbreviated or abbreviated syntax
Axis child attribute self parent Example unabbreviated child::price attribute::code self::node() parent::node() Example abbreviated price @code . .. //price

descendant-or-self /descendant-or-self()/price

The remaining axes only have unabbreviated syntax Example ancestor::node()

The first part of any location step is the axis, which determines the type of node your search will target. XPath defines 13 different types of axes. The following are the most commonly used axes: The child axis The attribute axis The self axis The parent axis The descendant-or-self axis

Using the Child Axis


A location step that uses the child axis returns the child nodes of the context node. The child axis is the default axis if none is specified in a location step. Example In this example, the child axis is used in two successive location steps.
child::catalog/child::product

From left to right, these location steps are evaluated as follows: child::catalog retrieves all children named catalog from the current context. child::product takes the node-set returned from the first location step and retrieves its child nodes named product. You could also write the preceding example by using an abbreviated syntax to achieve the same result:
catalog/product

28

Module 2: Selecting and Navigating Nodes Using XPath

Using the Attribute Axis


A location step that uses the attribute axis returns the attributes of the context node. This axis is empty unless the context node is an element. An @ symbol may be used as an abbreviated syntax for the attribute axis. Example In this example, a location path is made up of two location steps that use unabbreviated syntax. In the first location step, the child::product location path retrieves a node-set of child nodes named product. These are fed into the second location step, attribute::code, which retrieves the code attribute for each product.
child::product/attribute::code

This example can be written by using an abbreviated syntax, as follows:


product/@code

Using the Self Axis


The self axis contains only the context node. The location step . is short for self::node(). You will see situations later in the course where self is used to pass the current node as a parameter into a template rule.

Using the Parent Axis


A location step that uses the parent axis returns the parent of the context node, if there is one. All nodes except the root node have a single parent. The root node has no parent. Example This example selects the grandparent of the current node, and returns its <product> child elements.
parent::node()/parent::node()/child::product

The location steps are evaluated from left to right as follows: The first parent::node() retrieves the parent node; node() is a function that matches any node type. The second parent::node() retrieves the grandparent node. child::product retrieves its child elements named product. As is the case with the child axis specifier, the parent axis specifier can be written by using an abbreviated syntax. You can use .. to abbreviate the syntax for parent::node(). Again, the example can be rewritten by using an abbreviated syntax, as follows:
../../product

Module 2: Selecting and Navigating Nodes Using XPath

29

Using the Descendant-or-Self Axis


You use the descendant-or-self axis to return any descendants of the context node, plus the context node itself. The abbreviated syntax // is short for /descendant-or-self::node()/. Example This example returns all nodes named product that are descendants of the root.
//product

The next example returns all nodes named product that are descendants of a child node named catalog.
catalog//product

Using Other Axes


The remaining axes in XPath use only unabbreviated syntax. The following table shows how to use these axes in XPath.
Axis descendant ancestor ancestor-or-self following following-sibling preceding preceding-sibling namespace XPath axis syntax descendant::node() ancestor::node() ancestor-or-self::node() following::node() following-sibling::node() preceding::node() preceding-sibling::node() namespace::node()

30

Module 2: Selecting and Navigating Nodes Using XPath

Using Node Tests


Selecting nodes by name Specific name All nodes Name and namespace All nodes in namespace Selecting nodes by type axis::node-type-function() For example, using the child axis
text() text() node() node() processing-instruction() processing-instruction() comment() comment() Child examples price price * * aprefix:price aprefix:price aprefix:* aprefix:* Attribute examples @code @code @* @* @aprefix:code @aprefix:code @aprefix:* @aprefix:*

A node test appears after the axis specifier in a location step, and enables you to: Locate nodes by name. Locate nodes by type.

Locating Nodes by Name


You can search an axis for the specific name of a node. Use the following syntax to specify the name of the node you wish to select:
axis::name-of-node[predicate]

XPath will interpret the name differently, depending on the type of node called by the axis. If the axis is attribute, the name is treated as an attribute name. If you want to select all attribute nodes, use * instead of a specific name. If the axis is namespace, the name is treated as a namespace prefix. If you want to select all namespace nodes, use * instead of a specific namespace prefix. For all other axes, the name is treated as an element name. XPath will locate elements matching that name in the chosen axis. If you want to select all element nodes, use * instead of a specific name.

Module 2: Selecting and Navigating Nodes Using XPath

31

If you want to locate named elements or attributes in a particular namespace, specify the namespace prefix as follows:
axis::aprefix:name-of-node[predicate]

If you want to locate all elements or attributes in a particular namespace, use * rather than a particular element name or attribute name:
axis::aprefix:*[predicate]

Examples

Example price @code * @* aprefix:price @aprefix:code aprefix:* @aprefix:*

Explanation Select <price> elements. Select code attributes. Select all elements. Select all attributes. Select <price> elements defined in a namespace whose prefix is aprefix. Select code attributes defined in a namespace whose prefix is aprefix. Select all elements defined in a namespace whose prefix is aprefix. Select all attributes defined in a namespace whose prefix is aprefix.

Locating Nodes by Type


It is sometimes useful to be able to select nodes by type, rather than by name. Use the following syntax to create a location path that searches along an axis for a particular type of node:
axis::node-type()[predicate]

Use one of the following XPath functions in the place of node-type().


Function node() text() Selects All nodes on the specified axis. All text nodes on the specified axis. For example, child::text() selects all text nodes that are children of the context node, and descendant::text() selects all text nodes that are descendants of the context node. All comment nodes on the specified axis. All processing-instruction nodes on the specified axis. Optionally, you can pass a parameter to select processing-instructions with a specific name.

comment() processing-instruction()

32

Module 2: Selecting and Navigating Nodes Using XPath

Using Predicates to Filter Nodes


A location step may include zero or more predicates
axis::node-test[predicate] axis::node-test[predicate]

Filtering a node-set by using predicates Filter nodes by position Filter nodes by content
product[last()] product[last()] product[@code="123"] product[@code="123"]

Filter nodes by presence product[@code] product[@code] Multiple predicates may be defined Evaluated left to right
product[3][@discount] product[3][@discount]

A location step may include zero or more predicates. A predicate is a test condition enclosed in [brackets], and refines the set of nodes obtained by a node test. You can use predicates to filter nodes by position, content, or whether they contain specific nodes. The predicate is evaluated successively for each node in the node-set, to determine whether the node passes or fails the predicate test condition.

Filtering Nodes by Position


When filtering nodes by position, consider the following facts and guidelines: Most XPath axes number nodes in a forward document order. For example, if you are using the child axis, elements near the beginning of the document have a lower position than those closer to the end. You can filter nodes by using either a reverse axis or a forward axis. The reverse axes are ancestor, ancestor-or-self, preceding, and preceding-sibling. All other axes are forward axes. Each node in a node-set has a proximity position, indicating the position of the node in the node-set. The first position is 1. If a reverse axis is specified, nodes are numbered in reverse document order. If a forward axis is specified, nodes are numbered in forward document order.

Module 2: Selecting and Navigating Nodes Using XPath

33

Example

Example product[1] product[last()] /catalog/product[2]

Explanation Selects the first <product> child of the context node. Selects the last <product> child of the context node. Selects the second <product> child of the <catalog> element.

Filtering Nodes by Value


You can specify a predicate to filter nodes by value. The following example selects the <product> children of the context node that have a code attribute whose string-value is "123":
product[@code="123"]

Filtering Nodes by Presence


You can specify a predicate to filter nodes by presence, as detailed in the following table.
Example product[@code] product[@code and @discount] Explanation Selects the <product> children of the context node that have a code attribute. Selects the <product> children of the context node that have a code attribute and a discount attribute.

Defining Multiple Predicates


Multiple predicates may be defined, each in its own set of square brackets, to apply multiple filters to a node-set, as shown in the following table. Predicates are evaluated from left to right.
Example product[3][@discount] product[@discount][3] Explanation Selects the third <product> child if it has a discount attribute. Selects the third <product> child of the context node that has a discount attribute.

34

Module 2: Selecting and Navigating Nodes Using XPath

Demonstration: Selecting Nodes with Location Paths

In this demonstration, you will see how to use XPath location paths to locate nodes in an XML document. The demonstration makes use of the following XML document:
<catalog> <product code="123"> <description>Blue jeans</description> <price>35.99</price> </product> <product code="456"> <description>Plaid shirt</description> <price>22.95</price> </product> <product code="789"> <description>Leather jacket</description> <price>185.00</price> </product> <product code="321"> <description>Silk tie</description> <price>32.50</price> </product> <product code="654"> <description>Wool sweater</description> <price>27.99</price> </product> <product code="987"> <description>Patent shoes</description> <price>87.00</price> </product> </catalog>

Module 2: Selecting and Navigating Nodes Using XPath

35

Using Operators and Functions in Location Paths


Using Node-Set Expressions and Functions Using Boolean Expressions and Functions Using Number Expressions and Functions Using String Expressions and Functions

When writing XPath expressions, you can make them more selective and flexible by including any of a variety of XPath operators and functions. Many operators and functions defined by XPath are similar to those defined in other programming languages. In addition to the standard data typesBoolean, numeric, and stringXPath provides a node-set data type, which is the result of a location path. XPath provides functions and operators to work with nodesets as well as Boolean, numeric, and string data. In this section, you will learn how to write XPath expressions that make use of the operators and functions defined in the XPath specification.

36

Module 2: Selecting and Navigating Nodes Using XPath

Using Node-Set Expressions and Functions


Location paths return node-sets Merge node-sets within a location path by using the union operator ( | )
cd cd | | dvd dvd /*/(cd /*/(cd | | dvd) dvd) (cd (cd | | dvd)/@capacity dvd)/@capacity

Node-set functions can also be used


cd[position() cd[position() = = last()] last()] count(cd[@capacity count(cd[@capacity > > 5.8]) 5.8])

The node-set data type is unique to XPath. A node-set contains all of the nodes that match the location paths selection criteria. You can merge node-sets within a location path by using either the union operator or the node-set functions.

Merging Node-Sets with the Union Operator


Node-sets may be merged together by using the | union operator. Examples
Example cd | dvd Explanation This location path returns a node-set containing all of the <cd> or <dvd> child elements. This location path returns a node-set containing the capacity attribute of all <cd> or <dvd> elements. This location path returns a node-set containing all of the <cd> or <dvd> elements that are grandchildren of the root node. This location path returns a node-set containing all of the element and attribute nodes in the document.

(cd | dvd) / @capacity

/*/(cd | dvd)

//* | //@*

Module 2: Selecting and Navigating Nodes Using XPath

37

Node-Set Functions
Node-set functions either accept a node-set parameter, return a node-set, or return information about a particular node within a node-set, as described in the following table.
Function position() last() count() id() local-name() Description Returns the position of a node in a nodeset (starting at 1). Returns true if the node is the last node in the node-set. Returns the number of nodes in a nodeset. Selects nodes with a specific ID. Returns the local part of a node name, stripped of any namespace information. This is useful if you want to find all elements or attributes with the same name, regardless of their namespace. Returns the namespace URI of a node name. Returns the namespace prefix of a node name. Description The position() function is used here to return the final <cd> child element of the context node. The count() function returns the number of <cd> elements that have a capacity attribute value equal to 250. The id() function selects the element with a unique ID of myID. The local-name() function selects all child elements whose local-name is "cd". The namespace-uri() function selects all child elements that inhabit the "www.nwtraders.msft " namespace. The name() function selects all child elements with an expanded name of abc:cd

namespace-uri() name()

Examples

Example cd[(last)]

count(cd[@capacity = 250])

id("myID") *[local-name()="cd"] *[namespaceuri()="www.nwtraders.msft"] *[name()="abc:cd"]

Tip Avoid using name() to select all child elements within a certain namespace. Instead, use namespace-uri() and test the namespace URI rather than using name() to test its prefix.

38

Module 2: Selecting and Navigating Nodes Using XPath

Using Boolean Expressions and Functions


Boolean operators = != > >= < <=
product[(price product[(price > > 25 25 and and price price < < 35) 35) or or price price > > 50] 50]

Boolean functions
true() true() false() false() boolean() boolean() not() not() lang() lang()

Example
product[not(@code) product[not(@code) and and not(discount-policy)] not(discount-policy)]

XPath supports a Boolean data type for values that can be true or false.

Boolean Operators
Operator = and != > and >= and < and <= Description Used to apply equality and inequality when evaluating expressions. Used to apply greater-than, greater-than-or-equal, lessthan, less-than-or-equal operators when evaluating expressions. Returns true if both operands are true, false otherwise. The second operand is not evaluated if the first operand is false. Returns true if either (or both) operands are true, false otherwise. The second operand is not evaluated if the first operand is true. Explanation Selects <product> elements with <price> child elements with a value not equal to 25. Selects <product> elements with <price> child elements with a value less than 25. Selects <product> elements with <price> child elements with a value less than 10 or greater than 100.

and

or

Examples

Example product[price != 25] product[price < 25] product[price < 10 or price > 100]

Module 2: Selecting and Navigating Nodes Using XPath

39

Boolean Functions
Function true() false() boolean() Description Return the values true and false, respectively. One of the few uses of this function is to pass a Boolean parameter into an XSLT template rule. This is discussed in Module 5, Programming with XSLT. Converts an XPath data type into a Boolean value. A node-set is converted to false if it is empty, true otherwise. A number is converted to false if it is zero, true otherwise. A string is converted to false if it has zero characters, true otherwise. Returns the values true or false. If the parameter is a node-set, a number, or a string, it is converted to a Boolean first according to the boolean() function. Takes a string parameter representing a language encoding (such as "en"), and returns true if the context node (or one of its ancestors) defines an xml:lang attribute starting with the same language name. Explanation Returns false if there are no product elements with a code attribute of "123" in the document, true otherwise. Returns true always Returns true always. Returns true if the context node has no attributes. Returns true if the context node has no child nodes. Selects all child nodes except <description> nodes.

not()

lang()

Examples

Example boolean(//product[@code="123"])

boolean(3.14) boolean("Welcome") not(@*) not(node()) *[not(self::description)]

Precedence Order
This table shows the precedence order, from highest to lowest, for comparison operators and Boolean operators.
Operator () [] / // < <= > >= = != | not() and or Operator name Grouping Filters Path operations Comparisons Comparisons Union Boolean not Boolean and Boolean or

40

Module 2: Selecting and Navigating Nodes Using XPath

Using Number Expressions and Functions


Unary negation operator
account[balance account[balance >= >= - overdraft-limit] overdraft-limit]

Binary number operators, + - * div mod


account[(balance account[(balance mod mod 100) 100) > > 0] 0]

Number functions
number() number() floor() floor() ceiling() ceiling() round() round() sum() sum()

Example
account[sum(transaction-amount) account[sum(transaction-amount) > > 0] 0]

XPath provides support for a floating-point number type. Numbers are 64-bit quantities in XPath, and follow the Institute of Electrical and Electronics Engineers, Inc. (IEEE) 754 standard. This standard defines special values for negative infinity, positive infinity, negative zero, positive zero, and not-anumber (NaN).

Number Operators
Operator + Explanation Unary negation operator. Inverts the sign of a number. Addition operator. Subtraction operator. Use white space around the subtraction operator to avoid confusion with node names that contain a hyphen. Multiplication operator. Division operator. Modulus operator. Returns the remainder from a truncating division.

* div mod

Module 2: Selecting and Navigating Nodes Using XPath

41

Examples

Example account[balance >= - overdraft-limit]

Explanation Selects <account> elements whose <balance> is greater than the negated value of the <overdraftlimit>. Returns <account> elements whose <balance> exceeds the <overdraft-limit> value by 100 or more. Returns <account> elements whose <balanceoverdraft-limit> is greater than 100. Returns 1.

account[balance - overdraft-limit > 100] account[balance-overdraft-limit > 100] -7 mod 3

Number Functions
Functions number() Description Converts an XPath data type into a number value. number() is most often used in a predicate, to ensure that the predicate is treated as a number rather than a Boolean. A string is converted to the nearest number, or NaN if it doesnt correspond to a numeric value. A node-set is first converted to a string, and then is converted to a number as previously described. A Boolean is converted to 1 if true, 0 if false. floor() ceiling() round() sum() Takes a parameter and returns the largest integer that is less than or equal to the numeric value of the parameter. Takes a parameter and returns the smallest integer that is greater than or equal to the numeric value of the parameter. Takes a parameter and returns the nearest integer to the numeric value of the parameter. (The fraction .5 is rounded up.) Takes a node-set parameter, converts the string-value of each node to a number, and then sums all the numbers.

42

Module 2: Selecting and Navigating Nodes Using XPath Example account[number(/branch/@oldest-account)] Explanation Retrieves the number value of the oldestaccount attribute in the <branch> element. This value is then used in the predicate to select the <account> element at that position in the document. Tests whether a value is numeric. If the value isnt numeric, number() returns NaN. Returns 7. Returns 8. Returns 8. Returns -7. Returns 8. Returns -7. Calculates the average price of all products in a catalog. This example is not as efficient as it might be, because the <price> elements node-set is built twice. XSLT variables could be used here to avoid duplicate processing. XSLT variables are discussed in Module 5, Programming with XSLT.

Examples

string(number(@password)) = "NaN"

floor(7.5) floor(-7.5) ceiling(7.5) ceiling(-7.5) round(7.5) round(-7.5) sum(catalog/product/price) div count(catalog/product/price)

Module 2: Selecting and Navigating Nodes Using XPath

43

Using String Expressions and Functions


String query functions
string-length(customer/@last-name) string-length(customer/@last-name) customer[ customer[ starts-with(@last-name, starts-with(@last-name, "Mac") "Mac") ] ] customer[ customer[ contains(@last-name, contains(@last-name, "Mac") "Mac") ] ]

String extraction and modification functions


concat(@last-name, concat(@last-name, "[", "[", @first-name, @first-name, "]") "]") substring(@middle-name, substring(@middle-name, 1, 1, 1) 1) substring-before("12/3/1964", substring-before("12/3/1964", "/") "/") or or substring-after substring-after normalize-space(" normalize-space(" To To be be or or not not to to be be ") ")

translate(str, translate(str, "aeiou", "aeiou", "AEIOU") "AEIOU")

The string data type represents a sequence of zero or more XPath characters. XPath provides a reasonable set of functions for manipulating string values.

String Functions
Function string() Description Takes a single parameter and converts it into a string. If the parameter is a node-set, the function returns the string-value of the first node (in document order). If the node-set is empty, an empty string is returned. If the parameter is a number, the function returns an appropriate string representation. If the parameter is a Boolean, the function returns the string "true" or "false" accordingly. Returns the length of the string. Takes two or more strings and returns the concatenation of these strings. Returns true if the first string parameter starts with the second string parameter. Returns true if the first string parameter contains the second string parameter at any position. Takes a source string, a numeric start position (starting at 1), and optionally a numeric length. Returns the substring starting at the specified start position and having the specified length. If no length is defined, the substring extends to the end of the source string

string-length() concat() starts-with() contains() substring()

44

Module 2: Selecting and Navigating Nodes Using XPath Function substringbefore() Description Takes two string parameters. The first parameter specifies the source string, and the second parameter specifies the substring to search for. Returns the part of the source string before the first occurrence of the substring. Takes two string parameters. The first parameter specifies the source string, and the second parameter specifies the substring to search for. Returns the part of the source string after the first occurrence of the substring. Removes leading and trailing white space from a string, and replaces multiple adjacent white spaces with a single space. Takes three parameters: a source string, a comparison string, and a replacement string. In the source string, any characters that are specified in the comparison string are replaced by the corresponding characters in the replacement string.

substringafter()

normalizespace() translate()

Examples

Several common XML string handling tasks are illustrated here.


Example string-length("Hello") string-length("&lt;Hello&gt;") concat( @last-name, "[", @firstname, "]" ) customer[ starts-with(@last-name, "Mac") ] customer[ contains(@last-name, "Mac") ] substring(@middle-name, 1, 1) substring-before("12/3/1964", "/") substring-after("12/3/1964", "/") translate(str, ":/\", "") normalize-space(" To be or not to be ") Explanation Returns 5. Returns 7. Concatenates a persons last-name and first-name attributes into a string of the form last-name [firstname] . Selects customers whose last-name attribute starts with "Mac". Selects customers that have "Mac" anywhere in their last-name attribute. Returns the first letter of a middle-name attribute. Returns "12". Returns "3/1964". Removes colons, forward slashes, and backward slashes from a string. Returns the string "To be or not to be".

Module 2: Selecting and Navigating Nodes Using XPath

45

Lab 2.2: Selecting and Navigating Nodes Using XPath

Objectives
After completing this lab, you will be able to: Use XPath location paths to locate content in an XML document. Construct a complex XPath location path to filter the content of the XML document. Use XPath expressions to analyze the content and structure of the XML data.

Prerequisites
Before working on this lab, you must have: Familiarity with VBScript. A working knowledge of the XML Document Object Model (DOM).

Scenario
In this lab, you will add VBScript to an HTML page in order to navigate an XML document by using XPath location paths. The location paths will contain multiple location steps, to define a path through the document. Each location step will define an axis, a node test, and zero or more predicates.

Starter and Solution Files


There are starter and solution files for this lab. Starter files are in the folder <install folder>\Labs\Lab02.2\Starter. Solution files are in the folder <install folder>\Labs\Lab02.2\Solution.

Estimated time to complete this lab: 60 minutes

46

Module 2: Selecting and Navigating Nodes Using XPath

Exercise 1 Selecting Attributes and Named Elements


In this exercise, you will use the selectSingleNode() method to select an element at a particular index position in an XML document. You will then obtain information about its attributes and child elements. To select an element at a particular index position 1. Open NavigateNodes.htm with Notepad and find the script function btn1_OnClick(). This function is called when the btn1 button is clicked in the HTML document. 2. Use the selectSingleNode() method of the Document object to select the fourth <product> element. 3. Use the MsgBox function to display the text property of this text node. 4. Open NavigateNodes.htm with Microsoft Internet Explorer. Click button 1. The message box displays the following text:
Silk tie 32.50 3 25

To select an attribute 1. In NavigateNodes.htm, locate the script function btn2_OnClick(). 2. Use the selectSingleNode() method to get the code attribute of the fourth <product> element. 3. Display the text property of this attribute node. 4. Open NavigateNodes.htm with Internet Explorer. Click button 2. The message box displays the following text:
321

To select a specific child element 1. In NavigateNodes.htm, locate the script function btn3_OnClick(). 2. Use the selectSingleNode() method to get the <description> element of the fourth <product> element. 3. Display the text property of this element node. 4. Open NavigateNodes.htm with Internet Explorer. Click button 3. The message box displays the following text:
Silk Tie

Module 2: Selecting and Navigating Nodes Using XPath

47

Exercise 2 Selecting Multiple Child and Descendant Elements


In this exercise, you will use the selectNodes() method to select node-sets containing multiple nodes. First, you will select child elements, and then you will select descendant elements. In each case, you will use a loop to examine the details for each node in the node-set. To select all child elements of a <product> 1. In NavigateNodes.htm, find the script function btn4_OnClick(). 2. Use the selectNodes() method of the Document object to select all child elements of the fourth <product> element. 3. Use a loop to display the baseName and text property for each element. Display a separate message box in each iteration of the loop. 4. Open NavigateNodes.htm with Internet Explorer. Click button 4. Three message boxes display the following text:
Element name: description Element value: Silk tie Element name: price Element value: 32.50 Element name: discount-policy Element value: 3 25

To select all descendant elements of a <product> element 1. In NavigateNodes.htm, find the script function btn5_OnClick(). 2. Use the selectNodes() method of the Document object to select all descendant elements of the fourth <product> element. 3. Use a loop to display the baseName and text property for each element. Display a separate message box in each iteration of the loop. 4. Open NavigateNodes.htm with Internet Explorer. Click button 5. Five message boxes display the following text:
Element name: description Element value: Silk tie Element name: price Element value: 32.50 Element name: discount-policy Element value: 3 25 Element name: items Element value: 3 Element name: discount Element value: 25

48

Module 2: Selecting and Navigating Nodes Using XPath

Exercise 3 Filtering Nodes and Merging Node-sets


In this exercise, you will use XPath predicates to selectively filter nodes according to various criteria. You will also use the node-set union operator (|) to merge node-sets together. To filter nodes using a simple predicate 1. In NavigateNodes.htm, locate the script function btn6_OnClick(). 2. Use the selectNodes() method of the Document object to get the code attribute for all <product> elements whose <price> child element is greater than 30.00. 3. Use a loop to display the text property for each attribute. 4. Open NavigateNodes.htm with Internet Explorer. Click button 6. Four message boxes display the following text:
123 789 321 987

To filter nodes using a complex predicate 1. In NavigateNodes.htm, locate the script function btn7_OnClick(). 2. Use the selectNodes() method to get the code attribute for all <product> elements whose <price> child element is greater than 30.00 and less than 50.00. 3. Use a loop to display the text property for each attribute. 4. Open NavigateNodes.htm with Internet Explorer. Click button 7. Two message boxes display the following text:
123 321

To filter nodes by the presence of child elements 1. Examine the XML document to see which <product> elements have a <discount-policy> child element. This products price is reduced if the<discount-policy> child element is present and if the customer buys a certain number of items. 2. In NavigateNodes.htm, locate the script function btn8_OnClick(). 3. Use the SelectNodes() method of the Document object to get the code attribute for all <product> elements that have a <discount-policy> child element. 4. Use a loop to display the text property for each code attribute. 5. Open NavigateNodes.htm with Internet Explorer. Click button 8. Two message boxes display the following text:
321 654

Module 2: Selecting and Navigating Nodes Using XPath

49

To merge node-sets 1. In NavigateNodes.htm, locate the script function btn9_OnClick(). 2. Use the selectNodes() method of the Document object to get the code attribute and the <discount> element for all <product> elements that have a <discount-policy> child element. 3. Use a loop to display the text property for each code attribute and <discount> element. Display a separate message box for each item of information. 4. Open NavigateNodes.htm with Internet Explorer. Click button 9. Four message boxes display the code attribute and <discount> element for the two products that have a discount policy, as follows:
321 25 654 15

50

Module 2: Selecting and Navigating Nodes Using XPath

If Time Permits Using the XPath Core Function Library


In this exercise, you will use the XPath core function library to define predicates that filter nodes by value. To select nodes based on the absence of content 1. In NavigateNodes.htm, locate the script function btn10_OnClick(). 2. Use the selectNodes() method to get the code attribute for all <product> elements that do not have a <discount-policy> child element. 3. Use a loop to display the text property for each attribute. 4. Open NavigateNodes.htm with Internet Explorer. Click button 10. Four message boxes display the following text:
123 456 789 987

To select nodes based on string value 1. In NavigateNodes.htm, locate the script function btn11_OnClick(). 2. Use the SelectNodes() method to get the <description> element for all <product> elements that have the substring sh in the description. 3. Use a loop to display the text property for each <description> element. 4. Open NavigateNodes.htm with Internet Explorer. Click button 11. Two message boxes display the following text:
Plaid shirt Patent shoes

To select nodes based on numeric value 1. In NavigateNodes.htm, locate the script function btn12_OnClick(). 2. Use the selectNodes() method to get the <description> element for all <product> elements that have an odd code attribute (as opposed to an even code attribute). 3. Use a loop to display the text property for each <description> element. 4. Open NavigateNodes.htm with Internet Explorer. Click button 12. Four message boxes display the following text:
Blue jeans Leather jacket Silk tie Patent shoes

Module 2: Selecting and Navigating Nodes Using XPath

51

Best Practices
Use abbreviated syntax for axes Easier to understand and maintain Avoid expensive selections such as // Use an explicit path instead; for example, /*/product Do not use count() to see if a node-set is empty Use last() instead Do not use name() to test a nodes name Use namespace-URI() and local-name() instead

Follow these best practices when using XPath.

Use Abbreviated Syntax


Of the 13 axes that XPath defines for use in location steps, 5 of the most commonly used axes can use an abbreviated syntax. Use the abbreviated syntax rather than the unabbreviated syntax to improve the readability of your code and to make it easier to write. Consider the following facts and guidelines when writing syntactically abbreviated location paths: The child axis is the default axis, so you should not need to use child:: explicitly. The @ abbreviated syntax should be used instead of the unabbreviated attribute:: syntax. The . abbreviated syntax should be used instead of the unabbreviated self::node() syntax. The .. abbreviated syntax should be used instead of the unabbreviated parent::node() syntax. The // abbreviated syntax should be used instead of the unabbreviated /descendant-or-self::node()/ syntax.

52

Module 2: Selecting and Navigating Nodes Using XPath

Avoid Expensive Selections


Always consider how much work an XPath processor will have to do to build a node-set. Location paths such as //product can be extremely expensive, because the processor has to search the entire document for <product> elements at any location. Where possible, use your knowledge of the tree structure to ease the load. For example, if <product> elements are always direct children of the document element, then the following location path is much better than simply //product:
/*/product

Do Not Use count() Unnecessarily


The count() function requires an XPath processor to iterate a node-set in order to count the number of nodes it contains. Using the count() function is particularly inefficient in many XSLT constructs, such as in an <xsl:for-each> loop. The last() function is more efficient than count() because the node-set is not recounted each time the loop increments.

Do Not Use name() to Test a Nodes Name


The name() function returns a nodes namespace prefix and its local name. It is not a good idea to rely on the namespace prefix, because you can specify any prefix in an XML document. It is the namespace URI that matters, and this can be tested by using the namespace-uri() function.

Module 2: Selecting and Navigating Nodes Using XPath

53

Review
Introducing XPath Accessing Node Information Navigating a Document Using Location Paths Using Expressions and Functions in Location Paths

1. How can XPath be used in the DOM to retrieve specific nodes from an XML document?

2. How does the XPath data model differ from the DOM data model, with respect to the properties available in each node?

3. How do you use location paths to select nodes in an XML document?

54

Module 2: Selecting and Navigating Nodes Using XPath

4. What is the difference between the child axis, the descendant axis, and the following axis?

5. Write an XPath expression that selects the first and last <product> elements at any position in an XML document.

You might also like