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> (根元素节点)
<author>J K. Rowling</author> (元素节点)
lang="en" (属性节点)
原子值
原子值是没有子节点或父节点的节点。
原子值示例
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>
同级节点
具有相同父节点的节点。
在以下示例中;title、author、year 和 price 元素都是同级节点
<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>