Crazy XML study notes (12)------------XPath
#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>
Examples of nodes in the above XML document:
<bookstore> (文档节点) <author>J K. Rowling</author> (元素节点) lang="en" (属性节点)
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"
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>
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>
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>
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>
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>
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>
Result | |||
---|---|---|---|
Select all ancestors of the current node ( parent, grandfather, etc.) | |||
Select all ancestors of the current node (parent, grandfather, etc.) and the current node itself | |||
Select all attributes of the current node | |||
Select all child elements of the current node. | |||
Select all descendant elements (children, grandchildren, etc.) of the current node. | |||
Select all descendant elements (children, grandchildren, etc.) of the current node as well as the current node itself. | |||
Select all nodes after the closing tag of the current node in the document. | |||
Select all namespace nodes of the current node | |||
Select the current node parent node. | |||
Select all nodes before the start tag of the current node in the document. | |||
Select all sibling nodes before the current node. | |||
Select the current node. |
例子 | 结果 |
---|---|
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 + 4 | 10 |
- | 减法 | 6 - 4 | 2 |
* | 乘法 | 6 * 4 | 24 |
p | 除法 | 8 p 4 | 2 |
= | 等于 | 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。 |
or | 或 | price=9.80 or price=9.70 | 如果 price 是 9.80,则返回 true。 如果 price 是 9.50,则返回 fasle。 |
and | 与 | price>9.00 and price<9.90 | 如果 price 是 9.80,则返回 true。 如果 price 是 8.50,则返回 fasle。 |
mod | 计算除法的余数 | 5 mod 2 | 1 |
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>
节点选取
我们将使用微软的 XML DOM 对象来载入 XML 文档,并使用 selectNodes() 函数从 XML 文档选取节点:
set xmlDoc=CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("books.xml") xmlDoc.selectNodes(路径表达式)
选取所有的 book 节点
下面的这个例子选取了 bookstore 元素下所有的 book 节点:
xmlDoc.selectNodes("/bookstore/book")
选取第一个 book 节点
下面的例子仅选取 bookstore 元素下第一个 book 节点:
xmlDoc.selectNodes("/bookstore/book[0]")
选取 price
下面的例子从所有的 price 节点选取文本:
xmlDoc.selectNodes("/bookstore/book/price/text()")
选取价格高于 35 的 price 价格
下面的例子会选取所有价格高于 35 的 price 节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/price")
选取价格高于 35 的 title 节点
下面的例子会选取所有价格高于 35 的 title 节点:
xmlDoc.selectNodes("/bookstore/book[price>35]/title")
以上就是疯狂XML学习笔记(12)------------XPath的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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,

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