XPath 节点

创建于 2024-12-03 / 28
字体: [默认] [大] [更大]

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元素是title、author、year和price的父元素:

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

子节点

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

在下面的例子中;title、author、year和price元素都是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元素的后代是book、title、author、year和price元素:

<bookstore>

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

</bookstore>

0 人点赞过