XPath是一种可以在XML文档中寻找信息的语言

(XPath is a language for finding information in an XML document. )

XPath是可以查找XML文档中的元素和属性

(XPath is used to navigate through elements and attributes in an XML document.)

XPath是W3C的XSLT的标准的一个主要组成部分(XPath is a major element in the W3C's XSLT standard )

• HTML / XHTML
• XML / XML Namespaces

• XPath is a syntax(语法) for defining parts of an XML document
• XPath uses path expressions(路径表达式) to navigate(导航,定位) in XML documents
• XPath contains a library of standard functions (包含一些标准函数库)
• XPath is a major element in XSLT (XSLT的主要构成部份)
• XPath is a W3C Standard (W3C的一个标准)

## XPath Path Expressions(路径表达式)

XPath uses path expressions to select nodes(节点) or node-sets(节点集) in an XML document. These path expressions look very much like the expressions you see when you work with a traditional computer file system.

XPath通过路径表达式来选择XML文档中的节点或节点集,这些表达式很像那些传统的电脑文件系统的表达式.

## XPath Standard Functions(标准函数)

XPath includes over 100 built-in functions. There are functions for string values, numeric values, date and time comparison, node and QName manipulation, sequence manipulation, Boolean values, and more.

XPath 包含100多个内建函数,这些函数可以返回字符串值,数值,日期和时间的比较值,节点和QName操作,因果关系操作,布尔值,还有其它等.

## XPath is Used in XSLT

XPath is a major element in the XSLT standard. Without XPath knowledge you will not be able to create XSLT documents.

XQuery and XPointer are both built on XPath expressions. XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators.

XQuery 1.0 和 XPath 2.0有共同的数据模式,支持相同的函数和操作

## XPath is a W3C Standard

XPath became a W3C Recommendation(推荐,提议) 16. November 1999.

XPath was designed to be used by XSLT, XPointer and other XML parsing(分析,解析) software.

XPath Terminology术语

In XPath, there are seven kinds of nodes(七种节点): element, attribute, text, namespace, processing-instruction, comment, and document (root) nodes.



<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>  <title lang="en">Harry Potter</title>  <author>J K. Rowling</author>   <year>2005</year>  <price>29.99</price></book>
</bookstore>



   (document node) J K. Rowling  (element node) lang="en"  (attribute node)

### Atomic values(原子值)

Atomic values are nodes with no children or parent.

 J K. Rowling "en"

### Items

Items are atomic values or nodes.

## Relationship of Nodes节点间关系

### Parent父

Each element and attribute has one parent.每个元素和属性只有一个父节点

In the following example; the book element is the parent of the title, author, year, and price:

   Harry Potter  J K. Rowling  2005  29.99

### Children子

Element nodes may have zero, one or more children.元素的节点可以是零个,一个或多个

In the following example; the title, author, year, and price elements are all children of the book element:

   Harry Potter  J K. Rowling  2005  29.99

### Siblings同胞,同科,平行

Nodes that have the same parent.具有相同父节点的节点

In the following example; the title, author, year, and price elements are all siblings:下例中,title, author, year, 和 price 元素都是同科

   Harry Potter  J K. Rowling  2005  29.99

### Ancestors祖先,根

A node's parent, parent's parent, etc.一个节点的父节点,父节点的父节点

In the following example; the ancestors of the title element are the book element and the bookstore element:

    Harry Potter  J K. Rowling  2005  29.99 

### Descendants胄

A node's children, children's children, etc.一个节点的子节点,子节点的子节点

In the following example; descendants of the bookstore element are the book, title, author, year, and price elements:

    Harry Potter  J K. Rowling  2005  29.99 

# XPath Syntax 语法

XPath uses path expressions to select nodes or node-sets in an XML document. The node is selected by following a path or steps.

XPath使用路径表达式在XML文档中选择节点或节点套,节点的选择通过以下方式

## The XML Example Document

XML例子

We will use the following XML document in the examples below.我们用下面的XML文档来做为范例

     Harry Potter  29.99   Learning XML  39.95 

## Selecting Nodes选择节点

XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps.

XPath使用路径表达式在XML文档中选择节点或节点套,节点的选择通过以下方式

The most useful path expressions are listed below:

 Expression Description nodename节点名 Selects all child nodes of the node 选择指定节点的所有子节点 / Selects from the root node　　　从根节点来始选择 // Selects nodes in the document from the current node that match the selection no matter where they are 在文档中从当前节点开始选择，不管它们在哪里，只要它们符合条件 . Selects the current node　　选择当前的节点 .. Selects the parent of the current node　选择当前节点的父节点 @ Selects attributes　　　选择属性

### Examples

In the table below we have listed some path expressions and the result of the expressions:

 Path Expression Result bookstore Selects all the child nodes of the bookstore element　选择bookstore元素的所有子节点 /bookstore Selects the root element bookstore 选择根节点bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! 注：如果路径以＂／＂开始，它通常表示一绝对路径 bookstore/book Selects all  book确良elements that are children of bookstore 选择bookstore所有的book元素 //book Selects all book elements no matter where they are in the document 选择所有的book元素，无论它们在文档的什么地方 bookstore//book Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element 选择所有的bookstore的胄book 元素，不管它们在哪，只要在bookstore元素内 //@lang Selects all attributes that are named lang 选择所有的名字为lang的属性

## Predicates谓词

Predicates are used to find a specific node or a node that contains a specific value.

Predicates are always embedded in square brackets.

### Examples例

In the table below we have listed some path expressions with predicates and the result of the expressions:

 Path Expression Result /bookstore/book[0] Selects the first book element that is the child of the bookstore element. 选择bookstore元素的第一个book 元素 Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!! 注： IE5 和更高的版本指明[0]表示第一个节点，但是根据W3C的标准，应该是 [1]表示第一个节点 /bookstore/book[last()] Selects the last book element that is the child of the bookstore element 选择bookstore元素的最后一个book 元素 /bookstore/book[last()-1] Selects the last but one book element that is the child of the bookstore element 选择bookstore元素的倒数第二个book 元素 /bookstore/book[position()<3] Selects the first two book elements that are children of the bookstore element 选择bookstore元素的前两个book 元素 //title[@lang] Selects all the title elements that have an attribute named lang 选择所有具有lang属性的title元素 //title[@lang='eng'] Selects all the title elements that have an attribute named lang with a value of 'eng' 选择所有的，lang属性值为'eng'的 title 元素 /bookstore/book[price>35.00] Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 选择所有了bookstore 的 book 元素，且book 元素的price 元素的值大于35.00 /bookstore/book[price>35.00]/title Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 选择所有的bookstore元素的 book 的 title元素，且book 元素的price 元素的值大于35.00

## Selecting Unknown Nodes

XPath wildcards can be used to select unknown XML elements.

 Wildcard Description * Matches any element node　匹配任何元素节点 @* Matches any attribute node　匹配任何属性节点 node() Matches any node of any kind　匹配任何种类的节点

### Examples

In the table below we have listed some path expressions and the result of the expressions:

 Path Expression Result /bookstore/* Selects all the child nodes of the bookstore element 选择所有的bookstore元素的子节点 //* Selects all elements in the document 选择文档中所有的元素 //title[@*] Selects all title elements which have any attribute 选择的有属性的title元素

## Selecting Several Paths选择几条路径

By using the | operator in an XPath expression you can select several paths.

### Examples

In the table below we have listed some path expressions and the result of the expressions:

 Path Expression Result //book/title | //book/price Selects all the title AND price elements of all book elements 选择book元素的所有title 和price 元素 //title | //price Selects all the title AND price elements in the document 选择文档中所有的title 和price 元素 /bookstore/book/title | //price Selects all the title elements of the book element of the bookstore element AND all the price elements in the document 选择bookstore元素的book元素的所有title和 文档中所有的price 元素

# XPath Axes轴，轴线，轴心

XPath的主线

## The XML Example Document

We will use the following XML document in the examples below.

     Harry Potter  29.99   Learning XML  39.95 

## XPath Axes

An axis defines a node-set relative to the current node.

 AxisName Result ancestor Selects all ancestors (parent, grandparent, etc.) of the current node 选择当前节点的所有的祖先，根，（父，父之父） ancestor-or-self Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself 选择当前节点所有的祖节点和自己 attribute Selects all attributes of the current node 选择当前节点的所有属性 child Selects all children of the current node 选择当前节点的所有子节点 descendant Selects all descendants (children, grandchildren, etc.) of the current node 选择当前节点的所有胄（子，子之子等） descendant-or-self Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself 选择当前节点的所有子节点和自己 following Selects everything in the document after the closing tag of the current node 选择当前节点结束后的文档中的所有节点 following-sibling Selects all siblings after the current node 选择当前节点后的所有同科节点 namespace Selects all namespace nodes of the current node 选择当前节点的所有的名字空间 parent Selects the parent of the current node 选择当前节点的所有父节点 preceding Selects everything in the document that is before the start tag of the current node 选择文档中当前节点前的所有节点 preceding-sibling Selects all siblings before the current node 选择当前节点前的所有同科节点 self Selects the current node 选择当前节点

## Location Path Expression位置表达式

A location path can be absolute or relative.

An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:

 An absolute location path: /step/step/... A relative location path: step/step/...

Each step is evaluated against the nodes in the current node-set.

A step consists of:级构成：

• an axis (defines the tree-relationship between the selected nodes and the current node) 主线（规定一个当前节点和所先节点的关系树）
• a node-test (identifies a node within an axis) 一个节点测试（在主线中辨认一个节点）
• zero or more predicates (to further refine the selected node-set) 一个或多个谓词（进一步挑选选定的节点集）

The syntax for a location step is:语法

 axisname::nodetest[predicate]

### Examples

 Example Result child::book Selects all book nodes that are children of the current node 选择当前节点的所有的book节点 attribute::lang Selects the lang attribute of the current node 选择当前节点的lang 属性 child::* Selects all children of the current node 选择当前节点的所有子节点 attribute::* Selects all attributes of the current node 选择当前节点的所有属性 child::text() Selects all text child nodes of the current node 选择当前节点的所有文本子节点 child::node() Selects all child nodes of the current node 选择当前节点的所有子节点 descendant::book Selects all book descendants of the current node 选择当节点的所有book胄 ancestor::book Selects all book ancestors of the current node 选择当前节点的所有book节点的祖，根 ancestor-or-self::book Selects all book ancestors of the current node - and the current as well if it is a book node 选择当前节点的book元素的根节点或它自己（如果它也是一个book节点的话） child::*/child::price Selects all price grandchildren of the current node

# XPath Operators操作符

An XPath expression returns either a node-set, a string, a Boolean, or a number.

## XPath Operators

Below is a list of the operators that can be used in XPath expressions:

 Operator Description Example Return value | Computes two node-sets 返回两个节点集 //book | //cd Returns a node-set with all book and cd elements 返回一个所有book 元素和cd元素的节点集 + Addition加 6 + 4 10 - Subtraction减 6 - 4 2 * Multiplication乘 6 * 4 24 div Division除 8 div 4 2 = Equal等于 price=9.80 true if price is 9.80false if price is 9.90 != Not equal不等于 price!=9.80 true if price is 9.90false if price is 9.80 < Less than小于 price<9.80 true if price is 9.00false if price is 9.80 <= Less than or equal to小于等于 price<=9.80 true if price is 9.00false if price is 9.90 > Greater than大于 price>9.80 true if price is 9.90false if price is 9.80 >= Greater than or equal to大于等于 price>=9.80 true if price is 9.90false if price is 9.70 or or或 price=9.80 or price=9.70 true if price is 9.80false if price is 9.50 and and 且 price>9.00 and price<9.90 true if price is 9.80false if price is 8.50 mod Modulus (division remainder)取余 5 mod 2 1

# XPath Examples

Let's try to learn some basic XPath syntax by looking at some examples.

## The XML Example Document

We will use the following XML document in the examples below.

"books.xml":

    Everyday Italian Giada De Laurentiis 2005 30.00  Harry Potter J K. Rowling 2005 29.99  XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99  Learning XML Erik T. Ray 2003 39.95 

## Selecting Nodes　选择节点

We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:

 set xmlDoc=CreateObject("Microsoft.XMLDOM")xmlDoc.async="false"xmlDoc.load("books.xml") xmlDoc.selectNodes(path expression)

## Select all book Nodes选择所有的book节点

The following example selects all the book nodes under the bookstore element:

 xmlDoc.selectNodes("/bookstore/book")

## Select the First book Node选择第一个book节点

The following example selects only the first book node under the bookstore element:

 xmlDoc.selectNodes("/bookstore/book[0]")

If you have IE 5 or higher you can try it yourself

Note: IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!

### A Workaround!

To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.

The following example selects only the first book node under the bookstore element:

 xmlDoc.setProperty "SelectionLanguage", "XPath"xmlDoc.selectNodes("/bookstore/book[1]")

Try it yourself

## Select the prices

The following example selects the text from all the price nodes:

 xmlDoc.selectNodes("/bookstore/book/price/text()")

## Selecting price Nodes with Price>35

The following example selects all the price nodes with a price higher than 35:

 xmlDoc.selectNodes("/bookstore/book[price>35]/price")

## Selecting title Nodes with Price>35

The following example selects all the title nodes with a price higher than 35:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")

XSLT全称eXtended Stylesheet Language Transformation

xslt文件头
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="xpath">            该标签用于定义模版，同时分配给指定结点
<xsl:apply-templates select="xpath">    该标签用于指定要应用模版的结点

xsl:template中可以再次使用xsl:apply-templates，用于样式的多级嵌套

planets.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="planets.xslt"?>
<planets>
<planet color="red">
<name>Mercury</name>
<mass units="(Earth=1)">.0553</mass>
<day units="days">58.65</day>
<density units="(Earth=1)">.983</density>
<distance units="million miles">43.4</distance>
</planet>
<planet color="yellow">
<name>Venus</name>
<mass units="(Earth=1)">.815</mass>
<day units="days">116.75</day>
<density units="(Earth=1)">.943</density>
<distance units="million miles">66.8</distance>
</planet>
</planets>
planets.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="planet">
<p><xsl:value-of select="name"/></p>
</xsl:template>

<xsl:attribute>    可为html标签添加属性

<xsl:value-of select="xpath">   获得结点的值

1. 类似于if(){...}else{}的语法
<xsl:if test="expression/condition">

</xsl:if>

2. 类似于switch(){case n: ...}的语法
<xsl:choose>
<xsl:when test="condition"></xsl:when>
<xsl:when test="condition"></xsl:when>
<xsl:otherwise></xsl:otherwise>
</xsl:choose>
3.foreach语法
<xsl:for-each select="node1">

</xsl:for-each>
4.模版函数定义
<xsl:template name=”template name”>
<xsl:param name=”parameter1”/>
<xsl:param name="parameter2" select="defaultvalue"/>

</xsl:template>

</xsl:call-template>
5.模版函数调用
<xsl:call-template name="template name">
<xsl:with-param name="parameter1" select="parameter value"/>
...
</xsl:call-template>

• 在xml中引用xslt时，必须把type=”text/xml”改为type=”text/xsl”
• 必须先匹配根结点后，再匹配其他结点，否则可能无法显示，即match=”/”
• IE浏览器不支持任何XSLT默认规则，因此必须自己写

posted on 2007-05-29 16:29  Dragon-China  阅读(795)  评论(0编辑  收藏