Professional Documents
Culture Documents
Module 2: Selecting and Navigating Nodes Using Xpath
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.
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.
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.
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.
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.
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.
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.
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")
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.
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
<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.
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.
Comment Nodes
Comment nodes contain the comment text enclosed between the delimiters <!-and -->.
12
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.
13
Child nodes
Parent node
14
15
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
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
17
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.
18
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
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
21
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
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
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
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]
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.
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.
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
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
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
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
27
descendant-or-self /descendant-or-self()/price
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
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
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
29
The next example returns all nodes named product that are descendants of a child node named catalog.
catalog//product
30
A node test appears after the axis specifier in a location step, and enables you to: Locate nodes by name. Locate nodes by type.
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.
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
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.
comment() processing-instruction()
32
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.
33
Example
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.
34
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>
35
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
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.
/*/(cd | dvd)
//* | //@*
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])
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
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]
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"])
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
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
41
Examples
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.
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"
43
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
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
45
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.
46
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
47
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
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
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
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
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
52
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?
54
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.