Explain that students can either compile the query into an XPathExpression object and then use the Select method of the XPathNavigator object, or they can execute a string XPath expressi
Trang 1Contents
Overview 1 Lesson: Introduction to Querying XML
Module 5:
Querying XML
Trang 2Information in this document, including URL and other Internet Web site references, is subject to change without notice Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation
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
©2002 Microsoft Corporation All rights reserved
Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, SQL Server, Visual Basic, Visual C#, and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries
The names of actual companies and products mentioned herein may be the trademarks of their respective owners
Trang 3Instructor Notes
This module explains the process of querying an Extensible Markup Language (XML) document by using the XML Path Language (XPath) It explains how to write code to load an XML document and how to create a navigation cursor by
using an XPathNavigator object It also explains how to create your query by using an XPathDocument object and how to examine the result types In
addition, this module describes how to compile your query and evaluate your results
After completing this module, students will be able to:
Describe the business reasons for querying Extensible Markup Language (XML) documents by using the XML Path Language (XPath)
Cache XML data for querying by using an XPathDocument object
Create an XPathNavigator object to navigate through an XML document
Compile and evaluate the result of an XPathExpression object
Iterate through an XmlNodeList object returned by an XPath expression
To teach this module, you need the following materials:
Microsoft® PowerPoint® file 2663A_05.ppt
2663A_05_code.htm
Module 5, “Querying XML”
Instructor-led demonstration file
To prepare for this module:
Read all of the materials for this module
Complete the lab
Practice the instructor-led demonstration and code examination
The demonstrations in this module consist of a demonstration and code walkthrough of querying an XML document To prepare for these demonstrations, perform the demonstration steps as they are written and prepare to explain how the code works
In this module some of the PowerPoint slides provide hyperlinks that open a code samples page in the Web browser The code samples page provides a way
to show and discuss code samples when there is not enough space for the code
on the PowerPoint slide It also allows students to copy code samples directly from the browser window and paste them into a development environment All
of the linked code samples for this module are in a single htm file
To open a code sample, click the appropriate hyperlink on the slide To navigate between code samples in a particular language, use the table of contents
provided at the top of the code page Each hyperlink opens a separate instance
the Web browser, so it is a good practice to click Back in Internet Explorer
after you view a code sample This will close the browser window and return you to the PowerPoint presentation
Trang 4How to Teach This Module
This section contains information that will help you to teach this module
Lesson: Introduction to Querying XML Using XPath
This section describes the instructional methods for teaching each topic in this lesson
This lesson introduces XPath and the reasons for using it To understand the rest of the module, the students must understand how XPath expressions are a navigational language for XML documents Stress that XPath is an XML language that provides access to nodes and their contents, wherever the nodes are located in an XML document
The role of the World Wide Web Consortium (W3C) in the development of XPath in conjunction with the development of XQuery illustrates the dynamic nature of XML technologies and languages Stress that this evolution is ongoing and requires continual adjustments and refinements by both vendors and developers However, stress that the development is not random
This topic introduces students to the steps in the query process Emphasize that these steps are general For example, students might compile the query or they might not Explain that this information is covered in the third lesson of this module
In this demonstration, you will load a product catalog document and demonstrate multiple queries These queries have different return types, including node sets, numbers, strings, and Boolean values
Lesson: Creating and Navigating a Document Cache
This section describes the instructional methods for teaching each topic in this lesson
This topic explains why to use an XPathDocument object to cache data Emphasize that using the XPathDocument object provides a way for students
to query data in a read-only manner This topic also explains how to preserve
all white space in the source XML document Explain how XmlSpace.Preserve and XmlSpace.Default function
To navigate through an XML source document, students must create a cursor
by creating an XPathNavigator object In this topic, students will learn how to use the CreateNavigator() method to create an XPathNavigator object
This topic defines five methods that students can use to position the navigator
by traversing the XML document
Why Query XML Using
Trang 5Lesson: Executing Your Query
This section describes the instructional methods for teaching each topic in this lesson
In this topic, students will learn how to create and compile a query by using the
Compile() method Emphasize that one advantage of compiling a query is that
doing so allows you to determine the data type of the information returned by the query
This topic explains how to use the Evaluate() method to evaluate number return types Emphasize that the Evaluate() method returns a typed result
This topic explains how to use the Select() methods to evaluate node return
types Explain that students can either compile the query into an
XPathExpression object and then use the Select() method of the XPathNavigator object, or they can execute a string XPath expression directly
by using one of the Select() methods of the XPathNavigator object
Students might have noticed in the Select() method example in the previous topic that the resulting nodes are stored in an XPathNodeIterator This topic explains how to use an XPathNodeIterator to iterate through a node set Emphasize that students must call the MoveNext() method to move the XPathNodeIterator to the first node in the selected set
Explain the code underlying the application that you demonstrated at the beginning of this module
of knowing the right answers
1 If the students are not sure what method to call to move the
XPathNodeIterator to the first node in the selected set, review the third
4 This question explores the distinctions between the Evaluate() and Select()
methods
How to Create and
Compile a Query
How to Execute XPath
Expressions That Return
Numbers
How to Execute XPath
Expressions That Return
Node Sets
How to Iterate Through a
Node Set
Code Examination
Trang 6Lab 5.1: Querying XML Documents Using XPath
Before beginning this lab, students should have completed the module and answered the review questions Students will need to be able to perform most of the tasks that they learned in the lessons
Customization Information
This section identifies the lab setup requirements for a module and the configuration changes that occur on student computers during the labs This information is provided to assist you in replicating or customizing
Microsoft Official Curriculum (MOC) courseware
Trang 7Overview
Introduction to Querying XML Using XPath
Creating and Navigating a Document Cache
Executing Your Query
*****************************ILLEGAL FOR NON-TRAINER USE******************************
This module explains the process of querying an Extensible Markup Language (XML) document by using the XML Path Language (XPath) It explains how to write code to load an XML document and how to create a navigation cursor by
using an XPathNavigator object It also explains how to create your query by using an XPathDocument object and how to examine the result types In
addition, this module describes how to compile your query and evaluate your results Because an XPath expression can return a set of nodes, you will learn
how to iterate through a set of nodes by using an XPathNodeIterator object
After completing this module, you will be able to:
Describe the business reasons for querying XML documents by using XPath
Cache XML data for querying by using an XPathDocument object
Create an XPathNavigator object to navigate through an XML document
Compile and evaluate the result of an XPathExpression object
Iterate through an XmlNodeList object returned by an XPath expression Introduction
Objectives
Trang 8Lesson: Introduction to Querying XML Using XPath
What Is the Query Process?
Demonstration: Querying XML Using XPath
*****************************ILLEGAL FOR NON-TRAINER USE******************************
In this lesson, you will gain an understanding of XPath and how you can use XPath to locate parts of XML documents and navigate through a document that
is represented as a tree of nodes
After completing this lesson, you will be able to:
Describe the business reasons for querying XML documents by using XPath
Describe the steps in the querying process
Introduction
Lesson objectives
Trang 9Why Query XML Using XPath?
You can use XPath to:
Search
Filter
Summarize
XPath : XML :: Transact-SQL : SQL Server
Transact-SQL provides a standard language for manipulating relational database objects
XPath provides a standard language for navigating an XML document as a hierarchical tree of nodes
*****************************ILLEGAL FOR NON-TRAINER USE******************************
XPath is a programming language developed by the World Wide Web Consortium (W3C) specifically to address the nodes of an XML source XPath
is also well suited to querying an XML source You can use XPath to extract from an XML source whatever pattern of content or XML structure you require Some of the functionality of XPath is similar to the functionality of the
Document Object Model (DOM) For example, by using the DOM, you can locate a certain piece of XML data by node type However, the use of XPath to specify location information in an XML document is far more pervasive than the use of the DOM What then is XPath, and why is it the technology of choice for querying XML?
XPath was developed in part to correct some of the limitations inherent in querying XML by using the DOM XPath code that addresses and navigates through nodes is much simpler than similar code written for the DOM
XPath allows you to navigate through the nodes of an XML source You can also combine XPath navigation with other programming techniques For example, you can use XPath to locate parts of an XML document to generate results for a query with XQuery and XML Views in Microsoft®
SQL Server™ 2000
Introduction
Why use XPath?
Trang 10The following table illustrates how you can use XPath
Example Action Microsoft NET Framework objects used
Get all the <ProductName>
elements in a document with a price attribute value > $12.95
Search An XPathDocument object is
optimized for query Use an XPath expression to locate nodes
Change price information in
an XML product catalog
Locate information
to modify
An XmlDocument object provides an
implementation of the W3C DOM Use an XPath expression to locate nodes to be modified
Only find what matches a specified search criterion
Filter You can apply an Extensible
Stylesheet Language for Transformation (XSLT) style sheet to
an XPathDocument object The
XSLT style sheet often contains an XPath expression
Get a count of the number of parts that are out of stock
Summarize You can query an XPathDocument
object to provide a count or sum of information in the document, by using
an XPath expression
Examples of using
XPath
Trang 11What Is the Query Process?
MoveToRoot( ) MoveToNext( ) MoveToPrevious( ) MoveToFirstChild( )
XPathDocument CreateNavigator XPathNavigator
Evaluate( ) Select( )
Select( )
Compile( )
Boolean String Double
NodeSet
XPathNodeIterator MoveNext( ) For Each XPathExpression
5 Check the ReturnType
1 Load XPathDocument
2 Create XPathNavigator 4 Compile XPathExpression
3 Create the Query
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Querying by using XPath provides a way for your application to navigate from one node to the next, and to perform various operations along the way
To query an XML document, you generally perform the following steps:
1 Write code to load an XML document into an appropriate cache
You can use an XmlDocument object, XPathDocument object, DataSet,
or XmlDataDocument, depending on how you want to access the
information
2 Create a navigator to navigate through the XML document
You can use an XPathNavigator object to efficiently select nodes in an
XML document
3 Create the query
You generally create an XPath expression to select nodes in the cache You can execute a query directly, or you can compile it if you expect to run the query multiple times
4 Optionally, compile the query and determine the result type
XPath queries can return nodes, strings, or other types of values You must either know the types of results that you expect the query to produce, or determine the return type programmatically
If you compile the query, you can determine the type of information that will be returned before you execute the query In addition, compiling the query will result in faster execution
5 Evaluate the results
Depending on the type of information returned, you can process results by iterating through a set of nodes, or simply process the values returned
Introduction
Querying an XML
document
Trang 12Demonstration: Querying XML Using XPath
Load document - XPathDocument Create navigator - XPathNavigator Create query - XPathExpression
Optionally, compile query and examine result type
Optionally, compile query and examine result type
Evaluate results
Evaluate results
*****************************ILLEGAL FOR NON-TRAINER USE******************************
In this demonstration, you will see how you can query an XML document containing product catalog information The application:
Loads an XML document into a cache optimized for query
Creates a navigator to navigate nodes in the document
Compiles an XPath query and determines the return type
Evaluates the query and generates results
Processes and displays the results
You should also use this demonstration to review simple XPath location paths and functions
To load the product catalog document
1 In Microsoft Internet Explorer, open ProductCatalogData.xml and examine the structure of the document Notice that each category element contains category information and a product element containing all products in that category
2 In Microsoft Visual Studio® NET, open the project file for the Querying demonstration Open the C# or Microsoft Visual Basic® project file as appropriate
3 Run the application
4 Load the data file ProductCatalogData.xml by using the application The
application loads data from the file into an XPathDocument object
Introduction
Demonstration
Trang 13To compile and evaluate queries that return node sets
• For each XPath expression in the following table, click Evaluate XPath, examine the return type, and then click Execute XPath and explain the
results
any level
/node()/node() The element or text nodes that are
grandchildren of the current node /node()/node()/node() The element or text nodes that are
great-grandchildren of the current node//Product/ProductName All product name elements that are
descendants of a product element //AllCategories/Category/CategoryName All category names
//Category[CategoryID = 2]/
Products/Product/ProductName
All product names in category 2
To compile and evaluate queries that return numbers
• For each XPath expression in the following table, click Evaluate XPath, examine the return type, and then click Execute XPath and explain the
results
count(node()/node()) Number of children of the root element
count(//Category[CategoryID = 2]/
Products/Product)
Number of products in category 2
Trang 14To compile and evaluate queries that return strings
• The following XPath expression concatenates the first three grandchildren
nodes of the current node Click Evaluate XPath, examine the return type, and then click Execute XPath and explain the results
concat(node()/node()/node()[1],
" ",node()/node()/node()[2], " ",node()/node()/node()[3])
To compile and evaluate queries that return Boolean values
1 For each XPath expression in the following table, click Evaluate Xpath, examine the return type, and then click Execute XPath and explain the
results
starts-with(//Product/ProductName[1], "C") Does the first product name begin
with the letter “C”? (yes) starts-with(//Product/ProductName[1], "D") Does the first product name begin
with the letter “D”? (no)
2 Close the application
Trang 15Lesson: Creating and Navigating a Document Cache
What Is an XPathDocument Object?
How to Create a Navigator
How to Navigate Nodes in an XPath Document
*****************************ILLEGAL FOR NON-TRAINER USE******************************
In this lesson, you will gain an understanding of how to prepare to query an XML document To query an XML document, you must load the XML
document and create a navigator by creating an XPathNavigator object
After completing this lesson, you will be able to:
Load an XML document into an XPathDocument object
Create a navigator by creating an XPathNavigator object
Navigate an XPathDocument by using XPathNavigator methods
Introduction
Lesson objectives
Trang 16What Is an XPathDocument Object?
You can create an XPathDocument from:
*****************************ILLEGAL FOR NON-TRAINER USE******************************
Before you can query XML data, you must first load the data into a cache The Microsoft NET Framework contains several objects that can cache XML data
These include the XmlDocument, XPathDocument, and XmlDataDocument
objects The choice of which cache to use depends on what you need to do with the data
If you expect to query data in a read-only manner from an XML source, you
should use an XPathDocument object to cache the data The XPathDocument
object provides a read-only, in-memory cache for XML data that is optimized for querying with XPath and for applying XSLT style sheets You can also use
an XPathDocument object if you want to navigate through the individual
nodes in the document
You can also query, navigate, and apply XSLT transformations to data in an
XmlDocument object However, using the XmlDocument rather than the XPathDocument object is not as efficient for querying and applying style sheets XmlDocument is the NET Framework implementation of the DOM
and is intended for modifying the data
You can create a new XPathDocument object and populate it with data by
using the object’s constructor This constructor is overloaded to accept data
from a stream, file, TextReader, or XmlReader
Trang 17The following example illustrates how to create a new XPathDocument object:
the XPathDocument object, when the data source is a file or an XmlReader
The optional parameter XmlSpace can have the following values
Value Description
XmlSpace.Default Preserve only the significant white space
XmlSpace.Preserve Preserve all white space
XmlSpace.None Do not preserve white space
The following example illustrates how to preserve all white space in the source XML document:
// C#
//Create an XmlTextReader XmlTextReader CatalogReader;
//Code to populate the reader from a file…
Trang 18How to Create a Navigator
Create an XPathNavigator object
Use the CreateNavigator method
C# Example Visual Basic Example
*****************************ILLEGAL FOR NON-TRAINER USE******************************
To create a query, you must create a navigation cursor to navigate through the XML document you want to query You create a navigator by using the
CreateNavigator() method to create an XPathNavigator object
XPathNavigator provides read-only, random access to the data in the XML
document you loaded
The XPathNavigator object is based on the XPath data model and provides the
methods required to implement XPath queries over any data store You can also
use XPathNaivgator methods to traverse the nodes in the cache
To create an XPathNavigator object, use the CreateNavigator() method of the XPathDocument class The XPathNavigator class implements the
IXPathNavigable interface
Introduction
How to create an
XPathNavigator
Trang 19The following example creates an XPathNavigator object:
'Create and populate an XPathDocument Dim doc As XPathDocument = New XPathDocument(fileName) 'Create a navigator on the document
Dim nav As XPathNavigator = doc.CreateNavigator()
The XPathNavigator object has methods for selecting a set of nodes based on
criteria in an XPath expression You can also use methods to navigate in an
XPathDocument object in a way similar to how you traverse a DOM tree You will see how to use the Select() methods with an XPath expression in the
next lesson, Executing Your Query
You will see how to traverse nodes by using navigational methods in the next topic
Example
Methods for returning
nodes
Trang 20How to Navigate Nodes in an XPathDocument
Moves the navigator to the first child of
Moves the navigator to the next sibling
of the current node
C# Example Visual Basic Example
*****************************ILLEGAL FOR NON-TRAINER USE******************************
After you create an XPathNavigator over an XPathDocument, you can
traverse the nodes in the document or access individual nodes in a access manner You position the navigator on individual nodes by using various
random-methods of the XPathNavigator class
When you use an XPathNavigator, there is a concept of the current node You can set the current node in two primary ways: by using an XPathExpression object as part of a Select() method, or by using use navigational methods You will use the Select() methods in the next lesson You can use the following
navigational methods to position the navigator by traversing the XML
Method Action MoveToRoot() Moves the navigator to the root of the document
MoveToNext() Moves the navigator to the next sibling of the current node
MoveToPrevious() Moves the navigator to the previous sibling of the current
node
MoveToFirstChild() Moves the navigator to the first child of a node Only valid on
nodes that have children
MoveToParent() Moves the navigator to the parent of the current node Not
valid for the root node, because the root node has no parent
Introduction
How to traverse
document nodes by
using an XPathNavigator
Trang 21The following example uses the navigational methods of the XPathDocument
object to traverse the nodes in the document and print out properties and node values:
// C#
//Examine the current node in an XPathDocument //The XPathNavigator is positioned at the current node public static void RecursiveWalk(XPathNavigator nav) {
switch (nav.NodeType){
//The node is an element node //Print out information case XPathNodeType.Element:
if (nav.Prefix==String.Empty) MessageBox.Show(nav.LocalName);
else MessageBox.Show (nav.Prefix, nav.LocalName);
if ( nav.MoveToFirstChild() ) {
//If children exist, process the child recursively
do { RecursiveWalk(nav);
} //get the next sibling while ( nav.MoveToNext() );
//get the parent node nav.MoveToParent();
if (nav.NodeType == XPathNodeType.Element) MessageBox.Show (nav.Name);
} }
Code continued on the following page
Example of node
navigation
Trang 22'Visual Basic Public Shared Sub RecursiveWalk(nav as XPathNavigator) Select Case nav.NodeType
Case XPathNodeType.Element
If (nav.Prefix=String.Empty) Then MessageBox.Show(nav.LocalName) Else
MessageBox.Show(nav.Prefix,nav.LocalName) MessageBox.Show(nav.NamespaceURI)
End If Case XPathNodeType.Text MessageBox.Show(nav.Value) End Select
If (nav.MoveToFirstChild()) Then
Do RecursiveWalk(nav) Loop While (nav.MoveToNext()) nav.MoveToParent()
If (nav.NodeType = XPathNodeType.Element) Then MessageBox.Show (nav.Name)
End If End If End Sub
Trang 23Lesson: Executing Your Query
How to Create and Compile a Query
How to Execute XPath Expressions That Return Numbers
How to Execute XPath Expressions That Return Node Sets
How to Iterate Through a Node Set
*****************************ILLEGAL FOR NON-TRAINER USE******************************
After you load the XML document you want to query and create your navigator, you can define your search criteria, examine the result type, and evaluate your results In this lesson, you will see how to create and compile your query, how to evaluate your results, and how to iterate through those results if your return type is a node set
After completing this lesson, you will be able to:
Create a query by creating an XPathExpression object
Execute XPath expressions that return numbers
Execute XPath expressions that return node sets
Create an XPathNodeIterator object to iterate through node sets
Introduction
Lesson objectives
Trang 24How to Create and Compile a Query
To create a query, create an XPathExpression object
To create an XPathExpression object, use the Compile method of the XPathNavigator object
To determine the return type, examine the ReturnType property
Visual Basic Examples C# Examples
*****************************ILLEGAL FOR NON-TRAINER USE******************************
To create a query that you intend to execute more than once, you should create
an XPathExpression object The XPathExpression class encapsulates a compiled XPath expression You create an XPathExpression object when you
compile a query You can create and execute queries without compiling them,
as explained later in this module
There are two primary reasons for compiling a query rather than executing it
directly by using an XPathNavigator
If you compile a query before executing it, you can determine the data type of the information to be returned XPath queries can generate single nodes, node sets, strings, Booleans, or numbers It is important to know what information will be returned so that you can capture and process it properly
Another advantage of compiling the XPathExpression is that the query will
execute faster than the non-compiled version This is useful if you expect to execute the query multiple times in an application
Introduction
Why compile a query?
Trang 25To compile a query, you create an XPathExpression object and then use the Compile() method of the XPathNavigator object with an XPath query You set the XPathExpression object to the result
'Compile the expression by using the Compile() 'method of a populated XPathNavigator
xpe = nav.Compile("//book/author")
After you compile an XPathExpression, you can determine the data type of the result by using the ReturnType property The following table shows the possible values of the ReturnType and a description of each value
Description and enumerated value Value
String 0 Boolean 1 Number 2
Error 4 Any 5
How to compile a query
Determining the return
type