惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

博客园 - Scott Xu(南方小鬼)

C#发现之旅第一讲 C#-XML开发 XPath Operators Basic XPath Axes Basic XPath Basic XSLT Basic 深入浅出之正则表达式(二) 深入浅出之正则表达式(一) 通过Microsoft.Feeds获取feeds My Best Loves C#(3.0) 深入浅出系列之相关概念 C#(3.0) 深入浅出系列 Expression Studio 2.0 中文版发布了 一定能影响你的简单十句话 众说不一,“八零”后程序员到底怎么了? SQL Server 2005 实用例子 你最应该雇佣的程序员的十个特征 - Scott Xu(南方小鬼) 多个检索页面,分析,Silverlight绘图 .net component 开发系列1(学习) C# 中的设计模式3:Abstract Factory(学习笔记)
XPath Syntax Basic
Scott Xu(南方小鬼) · 2008-08-31 · via 博客园 - Scott Xu(南方小鬼)

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

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 元素