Table of Contents
What is XPath?
XPath path expression
XPath Standard Functions
XPath terminology
Node
Basic value (or atomic value, Atomic value)
Item
Node relationship
Parent
Children
Siblings (Sibling)
Ancestor
Descendant
位置路径表达式
绝对位置路径:
相对位置路径:
步(step)包括:
步的语法:
实例
XPath 运算符
XML实例文档
"books.xml" :
节点选取
选取所有的 book 节点
选取第一个 book 节点
选取 price
选取价格高于 35 的 price 价格
选取价格高于 35 的 title 节点
Home Backend Development XML/RSS Tutorial Crazy XML study notes (12)------------XPath

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

Feb 21, 2017 pm 02:47 PM


#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)!

 

 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles