次の方法で共有


Introduction to the Syntax of XPath (Windows Embedded CE 6.0)

1/6/2010

XML documents represent a hierarchy or tree of nodes, similar to the hierarchy of directories and files found in a file system. This accounts for the similarity of syntax between URLs and XML Path Language (XPath). Some analogous functionality is compared in the following table.

File system (URLs) Xpath

Hierarchy comprised of directories and files in a file system.

Hierarchy comprised of elements and other nodes in an XML document.

Files at each level have unique names. URLs always identify a single file.

Element names at each level might not be unique. XPath patterns identify a set of all the matching elements.

Evaluated relative to a particular directory, called the "current directory."

Evaluated relative to a particular node called the "context" for the query.

The following XML data represents a simple hierarchy, which will be used to demonstrate some of the basic query facilities of XPath.

<authors>
  <author>
    <name>Victor Hugo</name>
    <nationality>French</nationality>
  </author>
  <author period="classical">
    <name>Sophocles</name>
    <nationality>Greek</nationality>
  </author>
  <author>
    <name>Leo Tolstoy</name>
    <nationality>Russian</nationality>
  </author>
  <author>
    <name>Alexander Pushkin</name>
    <nationality>Russian</nationality>
  </author>
  <author period="classical">
    <name>Plato</name>
    <nationality>Greek</nationality>
  </author>
</authors>

XPath can use any node in the XML document as the context for a query. In an XSLT style sheet, the context for a query is the source node being currently processed by an <xsl:template> or <xsl:for-each> element. When using XPath directly from the DOM, you define the context by performing the query from a particular node. For more information about how a context is defined, see Defining the DOM Context for XPath Expressions and Defining the XSLT Context for XPath Expressions. The examples in this topic use the context of the root of the XML document.

A basic XPath pattern describes a path through the XML hierarchy with a slash-separated list of child element names. Starting from the document root, the following pattern traverses down through the hierarchy of the preceding sample XML document to the <name> elements.

authors/author/name

The XPath pattern identifies all elements that match the path. Because XSLT has the potential for identifying a set of nodes in a tree, it can be used as a simple query mechanism.

In addition to describing a path down a known hierarchy, XPath can include wildcards for describing unknown elements. An element of any name is represented by "*".

authors/*/name

The preceding query also identifies all the name elements, but does not require them to be children of an <author> element. Here is another example that finds both <name> and <nationality> elements.

authors/author/*

Branches on the path can be specified by using square brackets. The following query describes a branch on the <author> element, indicating that only <author> elements with nationality children should be considered.

authors/author[nationality]/name

This becomes even more useful for the sample data when comparisons are added. The following query returns the names of Russian authors. Note that comparisons can be used only within brackets.

authors/author[nationality='Russian']/name

Attributes are indicated in a query by preceding the name of the attribute with "@". The attribute can be tested as a branch off the main path, or the query can identify attribute nodes. The following examples return authors from the classical period, and just the two period attributes, respectively.

authors/author[@period="classical"]
authors/author/@period

For a complete list of XPath functionality supported by the Microsoft XML Parser (MSXML) for Windows Embedded CE, see XPath Syntax.

See Also

Concepts

XPath
Defining the DOM Context for XPath Expressions
Defining the XSLT Context for XPath Expressions