Crazy XML study notes (12)------------XPath

黄舟
Release: 2017-02-21 14:47:01
Original
1426 people have browsed it


#XPath is a language for finding information in XML documents. XPath is used to navigate through elements and attributes in XML documents.

What is XPath?

  • XPath uses path expressions to navigate in XML documents

  • XPath contains a library of standard functions

  • XPath is the main element in XSLT

  • XPath is a W3C standard

XPath path expression

XPath uses path expressions to select nodes or node sets in XML documents. These path expressions are very similar to those we see in regular computer file systems.

XPath Standard Functions

XPath contains over 100 built-in functions. These functions are used for string values, numeric values, date and time comparisons, node and QName processing, sequence processing, logical values, and more.


In XPath, there are seven types of nodes: elements, attributes, text, namespaces, processing instructions, comments, and documents node (or become the root node).

XPath terminology

Node

In XPath, there are seven types of nodes: Elements, attributes, text, namespaces, processing instructions, comments, and document (root) nodes. XML documents are treated as a tree of nodes. The root of the tree is called the document node or root node.

Please look at the following XML document:

<?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>
Copy after login

Examples of nodes in the above XML document:

<bookstore> (文档节点)
<author>J K. Rowling</author> (元素节点)
lang="en" (属性节点)
Copy after login

Basic value (or atomic value, Atomic value)

The basic value is a node with no parent or child.

Example of basic value:

J K. Rowling
"en"
Copy after login

Item

Item is a basic value or node.

Node relationship

Parent

Each element and attribute has a parent.

In the following example, the book element is the parent of the title, author, year and price elements:

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

Children

The element node can have zero, one or more sub.

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>
Copy after login
Copy after login
Copy after login

Siblings (Sibling)

Nodes with the same parent

In the following example, the title, author, year and price elements are all siblings:

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

Ancestor

The parent of a node, the parent of the parent, etc.

In the following example, the ancestors of the title element are the book element and the bookstore element:



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

Copy after login
Copy after login

Descendant

The child of a node, the child of the child, etc.

In the following example, the descendants of bookstore are the book, title, author, year and price elements:



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

Copy after login
Copy after login


XPath Axes

##XML instance document

We will use this XML document in the following example:

<?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>
Copy after login

XPath Axis

Axis defines a node relative to the current node set.

Axis nameResultancestorSelect all ancestors of the current node ( parent, grandfather, etc.)ancestor-or-selfSelect all ancestors of the current node (parent, grandfather, etc.) and the current node itselfattributeSelect all attributes of the current nodechildSelect all child elements of the current node. descendantSelect all descendant elements (children, grandchildren, etc.) of the current node. descendant-or-selfSelect all descendant elements (children, grandchildren, etc.) of the current node as well as the current node itself. followingSelect all nodes after the closing tag of the current node in the document. namespaceSelect all namespace nodes of the current nodeparentSelect the current node parent node. precedingSelect all nodes before the start tag of the current node in the document. preceding-siblingSelect all sibling nodes before the current node. selfSelect the current node.

位置路径表达式

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

绝对路径起始于正斜杠( / ),而相对路径不会这样。在两种情况中,位置路径均包括一个或多个步,每个步均被斜杠分割:

绝对位置路径:

/step/step/...
Copy after login

相对位置路径:

step/step/...
Copy after login

每个步均根据当前节点集之中的节点来进行计算。

步(step)包括:

  • 轴(axis)

  • 定义所选节点与当前节点之间的树关系

  • 节点测试(node-test)

  • 识别某个轴内部的节点

  • 零个或者更多谓语(predicate)

  • 更深入地提炼所选的节点集

步的语法:

轴名称::节点测试[谓语]
Copy after login

实例

例子结果
child::book选取所有属于当前节点的子元素的 book 节点
attribute::lang选取当前节点的 lang 属性
child::*选取当前节点的所有子元素
attribute::*选取当前节点的所有属性
child::text()选取当前节点的所有文本子节点
child::node()选取当前节点的所有子节点
descendant::book选取当前节点的所有 book 后代
ancestor::book选择当前节点的所有 book 先辈
ancestor-or-self::book选取当前节点的所有book先辈以及当前节点(假如此节点是book节点的话)
child::*/child::price选取当前节点的所有 price 孙。

XPath 运算符


XPath 表达式可返回节点集、字符串、逻辑值以及数字。

XPath 运算符

下面列出了可用在 XPath 表达式中的运算符:

运算符描述实例返回值
|计算两个节点集//book | //cd返回所有带有 book 和 cd 元素的节点集
+加法6 + 410
-减法6 - 42
*乘法6 * 424
p除法8 p 42
=等于price=9.80

如果 price 是 9.80,则返回 true。

如果 price 是 9.90,则返回 fasle。

!=不等于price!=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 fasle。

<小于price<9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 fasle。

<=小于或等于price<=9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 fasle。

>大于price>9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 fasle。

>=大于或等于price>=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.70,则返回 fasle。

orprice=9.80 or price=9.70

如果 price 是 9.80,则返回 true。

如果 price 是 9.50,则返回 fasle。

andprice>9.00 and price<9.90

如果 price 是 9.80,则返回 true。

如果 price 是 8.50,则返回 fasle。

mod计算除法的余数5 mod 21

XML实例文档

我们将在下面的例子中使用这个 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>
Copy after login

节点选取

我们将使用微软的 XML DOM 对象来载入 XML 文档,并使用 selectNodes() 函数从 XML 文档选取节点:

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

xmlDoc.selectNodes(路径表达式)
Copy after login

选取所有的 book 节点

下面的这个例子选取了 bookstore 元素下所有的 book 节点:

xmlDoc.selectNodes("/bookstore/book")
Copy after login

选取第一个 book 节点

下面的例子仅选取 bookstore 元素下第一个 book 节点:

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

选取 price

下面的例子从所有的 price 节点选取文本:

xmlDoc.selectNodes("/bookstore/book/price/text()")
Copy after login

选取价格高于 35 的 price 价格

下面的例子会选取所有价格高于 35 的 price 节点:

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

选取价格高于 35 的 title 节点

下面的例子会选取所有价格高于 35 的 title 节点:

xmlDoc.selectNodes("/bookstore/book[price>35]/title")
Copy after login

 

以上就是疯狂XML学习笔记(12)------------XPath的内容,更多相关内容请关注PHP中文网(www.php.cn)!

 

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template