什么是XPath?

 

Xpath 是一种能够在XML文档中寻找信息的语言。它通过XML文档中的元素和属性来进行导航。
XPath 是针对XML文档部分内容定义的语法
XPath 使用路径表达式在XML文档中导航
XPath 包含了一系列标准函数
XPath 在XSLT当中是一个主要的元素
XPath 是W3C标准

XPath 路径表达式:
XPath 使用路径表达式来选择XML文档中的nodes(节)或是node-set(节集)。这些路径表达式看上去与你平时所见的传统计算机文件系统路径非常地相似。

XPath 标准函数:
XPath包含了数量超过100的内置函数。这些函数针对字符串值,数字值,日期和时间比较,节操作,顺序操作,布尔值,等等。

XPath被用在XSLT:
在XSLT标准中XPath是主要的元素。没有XPath知识你将很难建立XSLT文档。
XQuery和XPointer都建立于XPath表达式。XQuery 1.0 和 XPath 2.0共享相同的数据模型并支持相同的函数和操作。

XPath是W3C标准:
XPath于1999年11月16日成为W3C的推荐标准。
XPath被设计成为用语XSLT,XPoniter以及其他XML解析的软件。

 

二、XPath Nodes(节)

 在XPath中有七种nodes(节):元素,属性,文字,命名空间,处理说明,注释,和文档(根)节。

XPath术语:
Nodes/节:XML文档被视为数状的节。树的根部被称为文档的节(或根节)。
Atomic values/原子值:原子值是那些没有子或父的节(无上下关系)。
Items/项目:项目是原子值或节。

Relationship of Nodes(节之间的关系):
Parent/父
Children/子
Siblings/兄
Ancestors/祖:节的父,父的父....都为祖
Descendants/孙:节的子,子的子...都为孙

 

三、XPath语法

 XPath使用路径表达式来选择XML文档的节或是节集。顺着路径或步骤来选择节。

举例中我们将使用下面的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(选择节):

一些非常有用的路径表达式:

表达式描述
nodenameSelects 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[选择属性]

(实例)下面我们所列举的表格有路径表达式以及其结果:

 

路径表达式结果
bookstoreSelects all the child nodes of the bookstore element[选择所有bookstore元素的子节]
/bookstoreSelects the root element bookstore

Note: If the path starts with a slash ( / ) it always represents an absolute path to an element!

[选择了bookstore的根元素。注意:如果路径的开始为(/)那此路径一定是到该元素的绝对路径]
bookstore/bookSelects all book elements that are children of bookstore[选择了所有在bookstore的子元素book元素所包含的所有元素(其实就为bookstore里book元素所包含的元素)]
//bookSelects all book elements no matter where they are in the document[选择所有为book元素的内容而不管book元素处于何处(有不同的父也没关系)]
bookstore//bookSelects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element[在bookstore元素内所有含有book元素的元素内容(只要book元素的祖元素为bookstore元素那都符合条件)]
//@langSelects all attributes that are named lang[选择所有属性名为lang的属性]

Predicates(谓语):

谓语用来指定明确的节所含有的特殊的值。
谓语被嵌入在中括号。

(实例)下面的表格列举了一些使用了谓语的路径表达式以及其产生的结果:

 

路径表达式结果
/bookstore/book[1] Selects the first book element that is the child of the bookstore element[选择了bookstore里的第一个book元素]
/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元素里price元素内容大于35.00的book元素]
/bookstore/book[price>35.00]/titleSelects 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,并且其兄弟元素price的内容得大于35.00]

Selecting Unknown Nodes(选择未知的节):

XPath的通配符可以用来选择未知的XML元素

 

通配符描述
*Matches any element node[相吻合的所有元素节]
@*Matches any attribute node[相吻合的所有属性节]
node()Matches any node of any kind[吻合任何类型的节]
(实例)下面的表格我们将列举一些路径表达式以及它们的结果 :

 

路径表达式结果
/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(选择数个路径):

通过在XPath中使用 | 你可以选择数个路径: (实例)下面的表格我们会列举一些路径表达式以及其结果:

 

路径表达结果
//book/title | //book/priceSelects all the title AND price elements of all book elements[选择所有book里title和price元素]
//title | //priceSelects all the title AND price elements in the document[选择所有title和price元素]
/bookstore/book/title | //priceSelects all the title elements of the book element of the bookstore element AND all the price elements in the document[选择所有book里的title元素和所有price元素]

四、XPath轴

举例中我们将使用下面的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>

XPath Axes(XPath轴):

轴定义了相对于当前节的节集。

轴名结果
ancestorSelects all ancestors (parent, grandparent, etc.) of the current node[选择了当前节的所有祖(父,祖父,等等)]
ancestor-or-selfSelects all ancestors (parent, grandparent, etc.) of the current node and the current node itself[选择当前节的所有祖并且还有当前节自己]
attributeSelects all attributes of the current node[选择所有当前节的属性]
childSelects all children of the current node[选择所有当前节的子]
descendantSelects all descendants (children, grandchildren, etc.) of the current node[选择所有当前节的孙(子,孙子,等等)]
descendant-or-selfSelects all descendants (children, grandchildren, etc.) of the current node and the current node itself[选择当前节的所有孙以及它本身]
followingSelects everything in the document after the closing tag of the current node[选择所有在关闭当前节标签后的所有内容]
following-siblingSelects all siblings after the current node[选择所有当前节后的兄]
namespaceSelects all namespace nodes of the current node[选择所有当前节的命名空间]
parentSelects the parent of the current node[选择当前节的父]
precedingSelects everything in the document that is before the start tag of the current node[选择当前节之前的所有内容]
preceding-siblingSelects all siblings before the current node[选择所有当前节之前的兄]
selfSelects the current node[选择当前节]

Location Path Expression(路径表达试定位):

定位路径可以是绝对的也可以是相对的。

绝对定位的路径由(/)开始,而相对定位就不这样。定位的路径由一个或多个步骤所组成,每部分由(/)相分隔:

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)[0个或多个谓语可以来更好的选择节]

定位的语法:

    axisname::nodetest[predicate]
(实例) :
Example结果
child::bookSelects all book nodes that are children of the current node[选择当前节点下所有为book的子节点]
attribute::langSelects 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::bookSelects all book descendants of the current node[选择当前节点所有为book的孙节点]
ancestor::bookSelects all book ancestors of the current node[选择所有当前祖节点为book的节点]
ancestor-or-self::bookSelects all book ancestors of the current node - and the current as well if it is a book node[当前节点和其祖节点为book的节点]
child::*/child::priceSelects all price grandchildren of the current node[当前节点所有含price的孙子节点]

五、XPath运算符

Xpaht表达式的返回值是:node-set、字符串、boolean、数值。

Xpath里使用的运算符:

 

OperatorDescriptionExampleReturn value
|Computes two node-sets//book | //cdReturns a node-set with all book and cd elements
+Addition6 + 410
-Subtraction6 - 42
*Multiplication

6 * 4

24
divDivision8 div 42
=Equalprice=9.80true if price is 9.80
false if price is 9.90
!=Not equalprice!=9.80true if price is 9.90
false if price is 9.80
<Less thanprice<9.80true if price is 9.00
false if price is 9.80
<=Less than or equal toprice<=9.80true if price is 9.00
false if price is 9.90
>Greater thanprice>9.80true if price is 9.90
false if price is 9.80
>=Greater than or equal toprice>=9.80true if price is 9.90
false if price is 9.70
ororprice=9.80 or price=9.70true if price is 9.80
false if price is 9.50
andand price>9.00 and price<9.90true if price is 9.80
false if price is 8.50
modModulus (division remainder)5 mod 21

六、精彩实例

让我们来尝试通过观察一些实例来学习基础的XPath语法。

我们将使用下面这个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>

 

选择节点:

我们使用了XMLDOM对象来加载XML文档并用selectNode()函数来进行XML文档上节点的选择:

var xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async
="false"
xmlDoc.load(
"books.xml")

xmlDoc.selectNodes(path expression)

选择所有book节点:

下面这个实例就会选择所有bookstore元素以下的book节点:

xmlDoc.selectNodes("/bookstore/book")

选择第一个book节点:

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

选择prices:

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

选择price大于35的price节点:

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

选择Price大于35的title节点:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")
 
<think>好的,用户想了解XPath的定义和用途,以及如何使用它。首先,我需要明确XPath的基本概念。XPath的全称是XML Path Language,主要用于在XML和HTML文档中导航和查找信息。根据引用[2],XPath的作用是在XML文档中查找信息,返回的是一个列表,这也适用于HTML,因为HTML可以被视为一种结构化的标记语言。 接下来,用户想知道如何使用XPath。根据引用[1]和引用[3]、[4],有多种使用场景和方法。例如,在浏览器中使用Chrome控制台或XPath Helper插件可以直接测试XPath表达式。另外,编程语言如Python的lxml库也支持XPath,用于数据提取。需要提到具体的例子,比如contains函数处理模糊匹配,以及命名空间的处理,这样用户能更清楚实际应用。 还需要注意用户可能的应用场景,比如网页数据抓取或自动化测试。引用[1]提到Selenium WebDriver使用XPath定位元素,这点可能对用户有帮助。此外,在线资源和教程如W3Schools也是学习的途径,应该推荐给用户。 用户可能需要具体的步骤,所以在回答中分点说明定义、用途和使用方法会更清晰。同时,生成的相关问题要覆盖常见疑问,比如语法、浏览器工具和编程库,这样用户可以进一步探索。 最后,确保回答符合系统级指令,数学公式使用正确的中文格式,并且引用正确来源。检查是否有遗漏的信息,比如XPathXML和HTML中的通用性,以及实际代码示例,这样用户能更好地理解和应用。</think>### XPath的定义与用途 **XPathXML Path Language)** 是一种用于在XML和HTML文档中定位和选择节点的查询语言。它通过路径表达式导航文档结构,并支持属性、文本内容等条件的筛选[^2][^4]。 #### 核心用途: 1. **数据提取**:从网页或XML文档中提取特定元素(如文本、属性值)[^2][^3]。 2. **自动化测试**:在Selenium等工具中定位页面元素以模拟用户操作[^1]。 3. **文档解析**:结合编程库(如Python的`lxml`)批量处理结构化数据[^3][^4]。 --- ### XPath使用方法 #### 1. **基础语法** - **路径表达式**: - `//`:从任意层级选择节点(如`//div`选择所有`<div>`)。 - `/`:从根节点或子节点选择(如`/html/body`)。 - **属性筛选**: - `[@属性名="值"]`:选择属性匹配的节点,例如`//a[@href="link.html"]`[^3]。 - **函数扩展**: - `contains(属性, "部分值")`:模糊匹配属性,如`//li[contains(@class, "li-first")]`[^3]。 #### 2. **浏览器工具实践** - **Chrome控制台**: 输入`$x('//h1')`可直接获取页面所有`<h1>`标签[^1]。 - **XPath Helper插件**: 高亮显示匹配结果,便于调试表达式[^1]。 #### 3. **编程语言集成(Python示例)** ```python from lxml import etree html = etree.HTML(网页源码) # 提取包含特定class的文本 result = html.xpath('//li[contains(@class, "li-first")]/a/text()') ``` #### 4. **命名空间处理** 若XML/HTML包含命名空间(如`xmlns="http://example.com"`),需声明映射: ```python namespaces = {'ns': 'http://example.com'} result = tree.xpath('//ns:book/ns:title', namespaces=namespaces)[^4] ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值