|
XPath is a set of syntax rules for defining parts of an XML document.
XPath is a major element in the W3C XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
Start learning XPath!
XPath Introduction
XPath is a set of syntax rules for defining parts of an XML document.
What is XPath?
- XPath is a syntax for defining parts of an XML document
- XPath uses paths to define XML elements
- XPath defines a library of standard functions
- XPath is a major element in XSLT
- XPath is not written in XML
- XPath is a W3C Standard
Like Traditional File Paths
XPath uses path expressions to identify nodes in an XML document. These path expressions look very much like the expressions you see when you work with a computer file system:
w3schools/xpath/default.asp
XPath Example
Look at this simple XML document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
</catalog>
The XPath expression below selects the ROOT element catalog:
/catalog
The XPath expression below selects all the cd elements of the catalog element:
/catalog/cd
The XPath expression below selects all the price elements of all the cd elements of the catalog element:
/catalog/cd/price
Note: If the path starts with a slash ( / ) it represents an absolute path to an element!
XPath Defines a Library of Standard Functions
XPath defines a library of standard functions for working with strings, numbers and Boolean expressions.
The XPath expression below selects all the cd elements that have a price element with a value larger than 10.80:
/catalog/cd[price>10.80]
XPath is Used in XSLT
XPath is a major element of the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.
You can read more about XSLT in our XSLT tutorial.
XPath is a W3C Standard
XPath was released as a W3C Recommendation 16. November 1999 as a language for addressing parts of an XML document.
XPath was designed to be used by XSLT, XPointer and other XML parsing software.
You can read more about XML and XSL standards in our W3C tutorial.
XPath Syntax
XPath uses path expressions to locate nodes within XML documents.
XML Example Document
We will use this simple XML document to describe the XPath syntax:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
</catalog>
Locating Nodes
XML documents can be represented as a tree view of nodes (very similar to the tree view of folders you can see on your computer).
XPath uses a pattern expression to identify nodes in an XML document. An XPath pattern is a slash-separated list of child element names that describe a path through the XML document. The pattern "selects" elements that match the path.
The following XPath expression selects all the price elements of all the cd elements of the catalog element:
/catalog/cd/price
Note: If the path starts with a slash ( / ) it represents an absolute path to an element!
Note: If the path starts with two slashes ( // ) then all elements in the document that fulfill the criteria will be selected (even if they are at different levels in the XML tree)!
The following XPath expression selects all the cd elements in the document:
//cd
Selecting Unknown Elements
Wildcards ( * ) can be used to select unknown XML elements.
The following XPath expression selects all the child elements of all the cd elements of the catalog element:
/catalog/cd/*
The following XPath expression selects all the price elements that are grandchild elements of the catalog element:
/catalog/*/price
The following XPath expression selects all price elements which have 2 ancestors:
/*/*/price
The following XPath expression selects all elements in the document:
//*
Selecting Branches
By using square brackets in an XPath expression you can specify an element further.
The following XPath expression selects the first cd child element of the catalog element:
/catalog/cd[1]
The following XPath expression selects the last cd child element of the catalog element (Note: There is no function named first()):
/catalog/cd[last()]
The following XPath expression selects all the cd elements of the catalog element that have a price element:
/catalog/cd[price]
The following XPath expression selects all the cd elements of the catalog element that have a price element with a value of 10.90:
/catalog/cd[price=10.90]
The following XPath expression selects all the price elements of all the cd elements of the catalog element that have a price element with a value of 10.90:
/catalog/cd[price=10.90]/price
Selecting Several Paths
By using the | operator in an XPath expression you can select several paths.
The following XPath expression selects all the title and artist elements of the cd element of the catalog element:
/catalog/cd/title | /catalog/cd/artist
The following XPath expression selects all the title and artist elements in the document:
//title | //artist
The following XPath expression selects all the title, artist and price elements in the document:
//title | //artist | //price
The following XPath expression selects all the title elements of the cd element of the catalog element, and all the artist elements in the document:
/catalog/cd/title | //artist
Selecting Attributes
In XPath all attributes are speci [1] [2] [3] [4] 下一页 [系统软件]给IE增加dom3xpath支持 [VB.NET程序]VBCOM TUTORIAL(3) [VB.NET程序]VBCOM TUTORIAL(2) [VB.NET程序]VBCOM TUTORIAL(1) [VB.NET程序]Microsoft Agent Tutorial Chapter 2 [VB.NET程序]Microsoft Agent Tutorial Chapter 1 [Web开发]XSLT轻松入门第四章:XPath的语法 [Web开发]XPath 2.0 的新特性 [Web开发]JDOM/XPATH编程指南 [Web开发].NET中的xpath
|