先看一下w3school上的定义,如下(自己翻译的,可能会有一点有妥):

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 )

并且XQuery和XPointer都是基于XPath而建立起来的(and XQuery and XPointer are both built on XPath expressions. )

参见:http://www.w3schools.com/xpath/default.asp




学习所需的基础知识:

  • HTML / XHTML
  • XML / XML Namespaces

什么是XPath?

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

没有XPath知识你将不能创建有效的,灵活的XSLT文档

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.

在 XPath中,共有七种节点:元素,属性,文本,名字空间,处理指令,注释和文档根节点

看下面这个例子:

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

上例中的节点

<bookstore>  (document node)
<author>J K. Rowling</author>  (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:

下例中book元素是title, author, year, 和 price的父节点:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

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:

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

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 元素都是同科

<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

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:

下例中,title元素的根是book和bookstore元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>

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:

下例中,bookstore元素的胄是book,title,author,year和price元素

<bookstore>
<book>
  <title>Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
</bookstore>





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文档来做为范例

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>


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.

在XPath表达式中通过使用"|"操作符可以来选择几条路径

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.

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>
<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>
</bookstore>


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 表达式可以返回一个节点集,一个字符串,一个布尔值,或一个数值


XPath Operators

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

下面是一些可以用在XPath表达式中的操作符的清单:

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.80
false if price is 9.90
!= Not equal不等于 price!=9.80 true if price is 9.90
false if price is 9.80
< Less than小于 price<9.80 true if price is 9.00
false if price is 9.80
<= Less than or equal to小于等于 price<=9.80 true if price is 9.00
false if price is 9.90
> Greater than大于 price>9.80 true if price is 9.90
false if price is 9.80
>=

Greater than or equal to大于等于

price>=9.80 true if price is 9.90
false if price is 9.70
or or或 price=9.80 or price=9.70 true if price is 9.80
false if price is 9.50
and and 且 price>9.00 and price<9.90 true if price is 9.80
false 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.

通过实例学习基本的xpath语法


The XML Example Document

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

使用下面的xml文档来做为范例

"books.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

View the "books.xml" file in your browser 点击查看  .


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:

我们通过使用Microsoft  的XMLDOM对象来加载XML文档和selectNodes() 函数来从XML文档中选择节点

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:

下例选择bookstore元素的所有的book 节点

xmlDoc.selectNodes("/bookstore/book")

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


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

注:W3C标准为使用[1]来选择第一个

A Workaround!

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

为了解决这个问题,你可以通过为XPath设置SelectionLanguage

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

选择price元素

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

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

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


Selecting price Nodes with Price>35

选择price值大于35的元素

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

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

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


Selecting title Nodes with Price>35

选择值price大于35的title元素

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,用于样式的多级嵌套
实例1:
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>
        <radius units="miles">1516</radius>
        <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>
        <radius units="miles">3716</radius>
        <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:apply-templates>,然后再使用<xsl:template>对planet结点作处理

<xsl:attribute>    可为html标签添加属性
实 例2:<a><xsl:attribute name="href" select="http://www.cnblogs.com"></a>该语句生成的结果为<a href="http://www.cnblogs.com"></a>

<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>
其中,parameter2使用select属性指定了默认值defaultvalue。
对于模版函数中的参数可以用$variable来引用
实例3
<xsl:call-template name="template1">
<xsl:with-param name="parameter1"/>
<xsl:value-of select="$parameter1"/>
</xsl:call-template>
5.模版函数调用
<xsl:call-template name="template name">
<xsl:with-param name="parameter1" select="parameter value"/>
...
</xsl:call-template>

在IE中使用xslt的注意点
  • 在xml中引用xslt时,必须把type=”text/xml”改为type=”text/xsl”
  • 必须先匹配根结点后,再匹配其他结点,否则可能无法显示,即match=”/”
  • IE浏览器不支持任何XSLT默认规则,因此必须自己写
添加javascript代码时的注意点
如果要在<script language="javascript"></script>中添加代码,必须使用<![CDATA[...]]>,因为添加的代码没有人任何标记,会使xslt文件不符合xml格式规范
posted on 2007-05-29 16:29  Dragon-China  阅读(912)  评论(0编辑  收藏  举报