原文:https://msdn.microsoft.com/zh-cn/library/ms256086(v=vs.80).aspx
本主题回顾整个 XPath 参考中出现的语法示例。所有示例均基于 XPath 语法的示例 XML 文件 (inventory.xml)。有关在测试文件中使用 XPath 表达式的示例,请参见本主题最后的“联合 ( | ) 示例”。
表达式 | 引用 |
---|---|
XML
./author
|
当前上下文中的所有 |
XML
author
|
当前上下文中的所有 |
XML
first.name
|
当前上下文中的所有 |
XML
/bookstore
|
本文档的文档元素 ( |
XML
//author
|
文档中的所有 |
XML
book[/bookstore/@specialty = @style]
|
style 属性值等于文档根目录的 |
XML
author/first-name
|
属于 |
XML
bookstore//title
|
|
XML
bookstore/*/title
|
属于 |
XML
bookstore//book/excerpt//emph
|
|
XML
.//title
|
当前上下文中更深的一级或多级的所有 |
XML
author/*
|
属于 |
XML
book/*/last-name
|
属于 |
XML
*/*
|
当前上下文的所有孙级元素。 |
XML
*[@specialty]
|
具有 specialty 属性的所有元素。 |
XML
@style
|
当前上下文的 style 属性。 |
XML
price/@exchange
|
当前上下文中 |
XML
price/@exchange/total
|
返回空节点集,因为属性不包含元素子级。XML 路径语言 (XPath) 语法允许使用此表达式,但是严格意义上讲无效。 |
XML
book[@style]
|
当前上下文的具有 style 属性的所有 |
XML
book/@style
|
当前上下文的所有 |
XML
@*
|
当前元素上下文的所有属性。 |
XML
./first-name
|
当前上下文节点中的所有 |
XML
first-name
|
当前上下文节点中的所有 |
XML
author[1]
|
当前上下文节点中的第一个 |
XML
author[first-name][3]
|
具有 |
XML
my:book
|
my 命名空间中的 |
XML
my:*
|
my 命名空间中的所有元素。 |
XML
@my:*
|
my 命名空间中的所有属性(不包括 my 命名空间中的元素的未限定属性)。 |
注意,索引相对于父级。考虑以下数据:
<x>
<y/>
<y/>
x>
<x>
<y/>
<y/>
x>
表达式 | 引用 |
---|---|
XML
x/y[1]
|
每个 |
XML
x/y[position() = 1]
|
每个 |
XML
(x/y)[1]
|
|
XML
x[1]/y[2]
|
第一个 |
其他示例引用 XPath 的示例 XML 文件。
表达式 | 引用 |
---|---|
book[last()] |
当前上下文节点的最后一个 |
XML
book/author[last()]
|
当前上下文节点的每个 |
XML
(book/author)[last()]
|
当前上下文节点的 |
XML
book[excerpt]
|
至少包含一个 |
XML
book[excerpt]/title
|
属于 |
XML
book[excerpt]/author[degree]
|
至少包含一个 |
XML
book[author/degree]
|
所有包含 |
XML
author[degree][award]
|
至少包含一个 |
XML
author[degree and award]
|
至少包含一个 |
XML
author[(degree or award) and publication]
|
至少包含一个 |
XML
author[degree and not(publication)]
|
至少包含一个 |
XML
author[not(degree or award) and publication]
|
至少包含一个 |
author[last-name = "Bob"] |
至少包含一个值为 Bob 的 |
XML
author[last-name[1] = "Bob"]
|
第一个 |
XML
author[last-name [position()=1]= "Bob"]
|
第一个 |
XML
degree[@from != "Harvard"]
|
from 属性不等于 "Harvard" 的所有 |
XML
author[. = "Matthew Bob"]
|
所有值为 Matthew Bob 的 |
XML
author[last-name = "Bob" and ../price > 50]
|
包含值为 Bob 的 |
XML
book[position() <= 3]
|
前三本书(1、2、3)。 |
XML
author[not(last-name = "Bob")]
|
不包含值为 Bob 的 |
XML
author[first-name = "Bob"]
|
至少包含一个值为 Bob 的 |
XML
author[* = "Bob"]
|
所有包含任何值为 Bob 的子元素的 author 元素。 |
XML
author[last-name = "Bob" and first-name = "Joe"]
|
所有包含值为 Bob 的 |
XML
price[@intl = "Canada"]
|
上下文节点中所有 intl 属性等于 "Canada" 的 |
XML
degree[position() < 3]
|
属于上下文节点子级的前两个 |
XML
p/text()[2]
|
上下文节点中每个 元素的第二个文本节点。 |
XML
ancestor::book[1]
|
与上下文节点最接近的 |
XML
ancestor::book[author][1]
|
与上下文节点最接近的 |
XML
ancestor::author[parent::book][1]
|
当前上下文中最接近的 |
为了演示 union 运算,我们使用以下 XPath 表达式:
x | y/x
在以下 XML 文件中选择所有值为 green 或 blue 的
xml version='1.0'?>
xml-stylesheet type="text/xsl" href="union.xsl"?>
<root>
<x>greenx>
<y>
<x>bluex>
<x>bluex>
y>
<z>
<x>redx>
<x>redx>
z>
<x>greenx>
root>
xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<xsl:for-each select="x | y/x">
<xsl:value-of select="."/>,
<xsl:if test="not(position()=last())">,xsl:if>
xsl:for-each>
xsl:template>
xsl:stylesheet>
green,blue,blue,green
encoding="UTF-16"?>green,blue,blue,green