search
HomeBackend DevelopmentXML/RSS TutorialCrazy 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>

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

相对位置路径:

step/step/...

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

步(step)包括:

  • 轴(axis)

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

  • 节点测试(node-test)

  • 识别某个轴内部的节点

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

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

步的语法:

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

实例

例子 结果
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

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 fasle。

小于或等于 price

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

如果 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)!

 

 

Statement
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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.