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

推荐订阅源

IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
雷峰网
雷峰网
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
L
LangChain Blog
人人都是产品经理
人人都是产品经理
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
P
Proofpoint News Feed
The Cloudflare Blog
D
Docker
大猫的无限游戏
大猫的无限游戏

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

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

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

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")

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

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

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

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]")

Select the prices选择price元素

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

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

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")