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



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

®

MSDN
®
at http://msdn.microsoft.com/xml.
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

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

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.


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

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

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.

!
The W3C XPath site at http://www.w3.org/tr/xpath
!
The W3C XQuery site at http://www.w3.org/XML/Query
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"

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 -->

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
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

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


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status