Tải bản đầy đủ (.pdf) (46 trang)

Tài liệu Module 6: Using XPath to Navigate a Tree of Nodes ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (986.98 KB, 46 trang )









Contents
Overview 1
Lesson: What Is XPath? 2
Lesson: Using XPath 11
Lesson: The Range of Application of
XPath 21
Lab 6: Using XPath to Navigate and
Select Data 27
Review 37

Module 6: Using XPath
to Navigate a Tree of
Nodes



Information 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, places or events 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.

 2001 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BackOffice, bCentral, BizTalk,
FrontPage, MSDN, MSN, Netshow, PowerPoint, SharePoint, Visio, Visual Basic, Visual C++,
Visual C#, Visual InterDev, Visual Studio, Windows Media, and Xbox are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.


Module 6: Using XPath to Navigate a Tree of Nodes iii


Instructor Notes
This module provides participants with an introduction to Extensible Markup
Language Path Language (XPath). This module is not an in-depth technical
module. Rather, it is designed to give an overview of the breadth of XPath
capabilities and enough technical examples to illustrate why XPath is a very
important part of the Extensible Markup Language (XML) technology set.
Participants can learn more through other courses and by reading books and the
XML and Extensible Stylesheet Language (XSL) Web page on Microsoft

®

MSDN
®
at
Be prepared to answer many questions about this module, because the topic can
be confusing.
After completing this module, participants will be able to:
!
Identify XPath expressions that are embedded in code and other places.
!
Identify the proper situations where you can use XPath.
!
Identify solutions where XPath is a necessary requirement or a critical
enabling technology.
!
Use XPath to navigate several documents and retrieve content from them
according to XPath characteristics.
!
Begin using XPath to navigate XML documents and select content in XML
documents.
!
Describe how it can be used by other XML technologies.

Presentation:
45 minutes

Lab:
30 minutes
iv Module 6: Using XPath to Navigate a Tree of Nodes



Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
To teach this module, you need the following materials:
• Microsoft PowerPoint
®
file 2500A_06.ppt

XPath is complex topic and requires some serious preparation to cover it
comfortably. To prepare for this module:
!
Read all of the materials for this module.
!
Review the references to XPath in the modules for XQuery, XSL
Transformations (XSLT), the Document Object Model (DOM), and SQL
XML.
!
Read the sections on XML in Microsoft SQL Server

2000 Books Online.
!
Complete the lab and the practice.
!
Read the sections of the World Wide Web Consortium (W3C) site on
XPath, XML Pointer Language (XPointer), XML Linking Language
(Xlink), and XQuery. Read the relevant Recommendation papers and Use
Case papers if you have time. There is a lot of information on this site, so
you will need to be selective. Check this site on a regular basis so that you

are not surprised by the questions of participants who do check it regularly.
!
Attend Course 1913A, Exchanging and Transforming Data Using XML and
XSLT, before teaching this course. In particular, be thoroughly comfortable
with Module 2 on XPath.
!
Work through as many examples of XPath expressions and uses of XPath as
you can so that you can give participants additional ideas about the uses of
XPath. To understand XPath, participants will need good verbal and visual
examples, so be ready to diagram XPath examples if required to clarify the
concepts.

Required Materials
Preparation Tasks
Module 6: Using XPath to Navigate a Tree of Nodes v


Instructor Setup for a Lab
This section provides setup instructions that are required to prepare the
instructor computer or classroom configuration for a lab.
!
Prepare for the lab
1. Be sure that SQL Server 2000 XML support in Internet Information
Services (IIS) is set up and working properly.
2. Be sure that the lab files are available.

Lab 6: Using XPath to
Navigate and Select
Data
vi Module 6: Using XPath to Navigate a Tree of Nodes



Module Strategy
This module is a preview of some of the content in subsequent modules on
XSLT, XQuery, and SQL XML. Be prepared to offer short description of the
functionality of XQuery, XSLT, and SQL XML. Be prepared to say how XPath
is used in each.
Use the following strategy to present this module:
!
What Is XPath?
This lesson introduces XPath and the reasons that you need it. To
understand the rest of the module, the participants must understand the
nature of XPath as 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 in an XML document.
The role of 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.
!
Using XPath
This topic describes the many facets of XPath. Concentrate on location
paths, node-sets, and the examples used to illustrate the nature of these
navigational techniques. Teach the complexity of XPath expressions
carefully. It is easy to overwhelm participants with the
axis::node-set[predicate] notation. Although this notation is concise,
XPath is not the friendliest of navigational languages.
The remainder of the module covers the broad range of additional operators
and functions that are available for use with XPath expressions. While these

are likely to be familiar to participants, they might find their role in XPath
more difficult to understand than their role in other languages, such as SQL.
Use the examples provided, and be prepared to provide additional examples
if participants are having trouble with these concepts.
Refer participants to Module 2 of Course 1913A, Exchanging and
Transforming Data Using XML and XSLT. This course provides a detailed
treatment of the XSLT.
Module 6: Using XPath to Navigate a Tree of Nodes vii


!
XPath and Other Technologies
The module started with the general need for XPath. The module concludes
with a return to that theme but in a more particular way. Each of the XML
technologies listed illustrates an area where XPath is needed for proper
navigation and manipulation of XML data documents.
Be prepared with extra examples for each technology listed if you have
time. The more you can do with extra material, the better your class will
understand the nature of XPath. Many people can understand XPath in the
abstract but have trouble with the concrete activity of creating and using
specific XPath expressions.
Use the practice to emphasize the way node navigation works with all of
these technologies. Be sure not to rush this practice, because it can easily
confuse those new to these concepts and technologies. Also, be sure to
make yourself familiar with the examples, structure, and function of the
practice files.
Use the practice to emphasize that the power of XML comes from
combining technologies rather than from using any single technology
by itself.



Module 6: Using XPath to Navigate a Tree of Nodes 1


Overview
!
What Is XPath?
!
Using XPath
!
The Range of Application of XPath

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
In this module, you will gain an understanding of XML Path Language (XPath)
and how you can use XPath to locate parts of Extensible Markup Language
(XML) documents and navigate through a document that is represented as a tree
of nodes.
After completing this module, you will be able to:
!
Identify XPath expressions that are embedded in code and other places.
!
Identify proper situations where you can use XPath.
!
Identify solutions where XPath is a necessary requirement or a critical
enabling technology.
!

Use XPath to navigate several documents and retrieve content from them
according to XPath characteristics.
!
Begin using XPath to navigate XML documents and select content in XML
documents.
!
Describe how it can be used by other XML technologies.

Introduction
Objectives
2 Module 6: Using XPath to Navigate a Tree of Nodes


Lesson: What Is XPath?
!
A Language for Document Addressing
!
What Are the Node Types in XPath?
!
What Is a Location Path?
!
Location Path Syntax

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
Some of the functionality of XPath is similar to the functionality of the
Document Object Model (DOM). For example, by using DOM, you can locate

a certain piece of XML data by node type. However, the use of XPath, for
specifying location information in an XML document, is far more pervasive
than the use of DOM. What then is XPath, and why is it the technology of
choice for querying XML?
After completing this lesson, you will be able to:
!
Define XPath.
!
State why you need XPath to work with XML documents.
!
Define a location path.
!
Explain location path syntax.
!
Choose between using unabbreviated and abbreviated XPath syntax.

Introduction
Lesson ob
jectives
Module 6: Using XPath to Navigate a Tree of Nodes 3


A Language for Document Addressing
!
Use XPath to address parts of an XML
source
!
Use XPath to match patterns of
content or structure
!

XPath includes an object model for
XML that maps XML element to a tree
of node types

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
To perform meaningful operations on an XML source, you must be able to:
!
Address and reference locations in an XML source.
!
Provide a means of matching patterns of locations.
!
Perform basic manipulation of XML content and structure.

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 are
interested in.
XPath allows you to navigate through the nodes of an XML source. You can
combine XPath navigation with other programming techniques.
For example, you develop an application that uses DOM to load an XML
document into memory, XPath to navigate and select nodes, and then DOM
again to change node content. You specify path information by using an XPath
location path and then you add, modify, or remove content by using DOM
methods.
In other cases, such as with XQuery and XML Views in Microsoft

®

SQL Server

2000, XPath is used to locate parts of an XML document to
generate results for a query. XPath enables you to navigate through the tree and
collect the data or metadata required to answer the query.
Introduction
What is XPath?
Navi
gation
4 Module 6: Using XPath to Navigate a Tree of Nodes


To find parts of XML documents, you must be able to look for patterns of
information in both data and metadata. XML Pointer Language (XPointer),
Extensible Stylesheet Language Transformations (XSLT), and XQuery depend
on the pattern matching capabilities of XPath.
One form of pattern matching is based on finding certain node characteristics,
such as node location, node name, or node type. Another form of node
matching is based on the data characteristics of element and attributes, such as
the element name or the attribute contents.
By using the data returned from pattern matching, you can perform calculations
and exchange data between data islands and applications for further processing.
The following are the answers to frequently asked questions about XPath.
Question Answer

Who developed XPath? TheW3C developed XPath.
What’s the current state of W3C
XPath activity?

XPath version 2 is currently under development.
How do you use XPath? You use XPath to provide a way for your
application to navigate from one node to the next,
performing operations along the way. XPath also
provides a way to address patterns of nodes.
Why is XPath necessary? It
seems like it does the same thing
as DOM.
XPath was developed in part to correct some of
the limitations inherent in querying XML by
using DOM. Code that addresses and navigates
through nodes that is written in XPath is much
simpler than code written for DOM.
When you build an XML solution,
do you typically use XPath alone
or with other XML technologies?
XPath is more typically used with other XML
technologies. Any XML solution that uses XSLT,
XQuery, or DOM relies heavily on XPath.

Pattern matching
Frequently asked
questions about XPath
Module 6: Using XPath to Navigate a Tree of Nodes 5


These key definitions help you understand XPath.
Term Definition

Expression Commands in the XPath language are called expressions.

An XPath expression is composed of location
information about the XML source and whatever
functions and variables are required to process the XML
specified in the location information.
Nodes Like DOM, XPath breaks an XML source into a tree of
nodes. For example, there is a node that corresponds to
the root element. There are nodes that correspond to any
element that is not the root element.
Axes The axis is an XPath expression parameter that you
specify. The axis parameter determines the type of node
to which the expression applies. The axes provide a
coordinate system for expressions. Axes specify location
information within an XML source.
Functions and variables Function and variable types in XPath are similar to those
of other programming languages.
Location path A location path is a special form of XPath expression
whose purpose is to specify path information. Although
XPath is capable of much greater functionality, most of
the solutions that use XPath incorporate location paths.
Result set A result set is what an XPath expression returns. Like
the results of any other type of query, a result set can be
empty, can contain a single node, or can contain many
nodes, depending on the path specified and the
document contents.

For more information about XPath, see the following:
!
Microsoft Visual Studio
®
.NET documentation

!
The W3C XPath site at
!
The W3C XQuery site at
This links to XPath 2.0 documents.
!
Course 1913A, Exchanging and Transforming Data Using XML and XSLT

XPath definitions
For more information
6 Module 6: Using XPath to Navigate a Tree of Nodes


What Are the Node Types in XPath?
"1"
"1"
"Beverages"
"Beverages"
Root
Root
"urn:northwind"
"urn:northwind"
"comment "
"comment "
"proc-instr"
"proc-instr"
<categoryname>
<categoryname>
"urn:northwind"
"urn:northwind"

<categories>
<categories>
<categories xmlns="urn:northwind">
<?proc instr?>
<!--comment-->
<categoryname id="1">Beverages</categoryname>
</categories>
<categories xmlns="urn:northwind">
<?proc instr?>
<!--comment-->
<categoryname id="1">Beverages</categoryname>
</categories>
!
Root
!
Element
!
Attribute
!
Namespace
!
Processing Instruction
!
Comment
!
Text
!
Whitespace

*****************************

ILLEGAL FOR NON
-
TRAINER USE
******************************
XPath treats XML sources as a collection of nodes of a specified type.
You use the node type as a parameter when building an XPath expression. The
following chart describes each of the node types.
Node type Description

Attribute An attribute in the node tree.
Comment A comment.
Element An element in the node tree.
Namespace A namespace node. This is equivalent to a DOM attribute
that declares a namespace (for example, xmlns).
ProcessingInstruction A processing instruction. This does not include XML
declarations.
Root The root of the node tree.
Text The text content of an element or attribute.
Whitespace A node with only white space characters.

Introduction
Definitions of each node
type
Module 6: Using XPath to Navigate a Tree of Nodes 7


Can you identify the XPath node types in the following sample XML?
<TestCases xmlns="urn:Test">
<?proc instr?>
<!-- Version 3.1 -->

<Test id="">
<Name></Name>
<Type></Type>
<Procedure>
<ProcedureName></ProcedureName>
<Step></Step>
</Procedure>
</Test>
</TestCases>

Practice
8 Module 6: Using XPath to Navigate a Tree of Nodes


What Is a Location Path?
!
Composed of one or more location steps
!
Read from left to right
!
Location step syntax (unabbreviated):
!
Axis types:
axis::node-test[predicate]
axis::node-test[predicate]
!
Node-test parameters:
!
Predicate options:
by node name or by node type

filter by position
filter by value
filter by presence
descendant::
following::
ancestor::
parent::
child::
preceding::
self::
descendant-or-self::
ancestor-or-self::
following-sibling::
preceding-sibling::attribute::
namespace::

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
A location path is an XPath expression that selects specified nodes in an XML
document. A location path is composed of one or more location steps. An
XPath processor evaluates the location path from left to right. Each location
step narrows the scope of the XPath statement.
A single location step is composed of three parts: an axis reference, a node-test,
and a predicate. A location path is composed of one or more location steps.
axis::node-test[predicate]…

XPath defines 13 different types of axes. Each axis provides you with a

different way to navigate the XPath node-set.
The node test is the pattern that XPath matches in the axis. The node test returns
a result set depending on the content of the axis. The node test predicate is
optional. The predicate allows the node test to match a node by node name or
by node value.

Names can be represented by:
Nodes can be represented by the
following functions:

The name itself, for example,
<categoryname>
The node function

Wildcard characters, for example, * or
@*
The text function

Names and namespaces, for example,
label:categoryname or @label:code
The comment function
The processing-instructions function
The predicate applies an additional filter to
the data. For example you can filter by
position, by value, or by presence.

Introduction
S
yntax of a location step
Module 6: Using XPath to Navigate a Tree of Nodes 9



Location paths can be absolute or relative. An absolute location path begins at
the root node of the XML document. A relative location path begins at the
context node. The context node is the node currently selected.
Assuming that you have an XML document named employees.xml, which
contains employee information like that found in the Northwind database, you
can use the following types of expressions.
Path type Syntax and Example

Absolute
/location-step/location-step/… etc


/employees/employee/fullname

Relative
Location-step/location-step/… etc


employee/fullname



Notice that the absolute path begins with a slash (/).

Relative or absolute
location paths
Importan
t

10 Module 6: Using XPath to Navigate a Tree of Nodes


Location Path Syntax
!
Choose between unabbreviated and abbreviated syntax
when specifying the axis
attribute::
attribute::
/descendant-or-self::node()
/descendant-or-self::node()
self::node
self::node
parent::node
parent::node
node tree root
node tree root
@
@
//
//
.
.
..
..
/
/
Unabbreviated syntax
Abbreviated syntax
ancestor-or-self::

ancestor-or-self::
not available
not available

*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
There are two forms of location step syntax.
!
The first is a long form that includes the axis name, a double colon, a node-
test, and an optional predicate.
!
The second is called abbreviated syntax. It uses symbols to represent the
most commonly used axis types. The slide shows what symbols correspond
to what axis types.

For most situations, you will use the abbreviated syntax. It is easier to work
with, because it is simpler to read and to write.
There is one situation where you must use the unabbreviated syntax. The
ancestor-or-self axis can only be expressed by using unabbreviated syntax.
Introduction
Determining the syntax
to use

×