目录

XPath 节点


XPath 术语

节点

在 XPath 中,有七种节点:元素节点、属性节点、文本节点、名称空间节点、处理指令节点、注释节点和根节点。

XML 文档被视为节点树。树的最顶层元素称为根元素。

请看下面的 XML 文档:

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>
  <book>
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

上面 XML 文档中的节点示例:

<bookstore> (root element node)

<author>J K. Rowling</author> (element node)

lang="en" (attribute node)

原子值

原子值是没有子节点或父节点的节点。

原子值的示例:

J K. Rowling

"en"

项目

项目是原子值或节点。



节点关系

家长

每个元素和属性都有一个父元素。

在下面的例子中; book 元素是标题、作者、年份和价格的父元素:

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

孩子们

元素节点可以有零个、一个或多个子节点。

在下面的例子中;标题、作者、年份和价格元素都是 book 元素的子元素:

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

兄弟姐妹

具有相同父节点的节点。

在下面的例子中;标题、作者、年份和价格元素都是同级元素:

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

祖先

节点的父节点、父节点的父节点等。

在下面的例子中; title 元素的祖先是 book 元素和 bookstore 元素:

<bookstore>

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

</bookstore>

后代

节点的子节点、子节点的子节点等。

在下面的例子中; bookstore 元素的后代是书籍、标题、作者、年份和价格元素:

<bookstore>

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

</bookstore>