XPath 节点
XPath节点
XPath中有七种节点:
元素
属性
文字
命名空间
加工指令
内容
文档节点。
可以将XML文档指定为节点树。树的最上层元素称为根元素。
让我们举一个例子: XML文档,以了解XPath节点的不同术语。
XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<Library>
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
</Library>
上述XML文档中的节点:
<library> (root element node)
<author>Chetan Bhagat</author> (element node)
lang="en" (attribute node)
原子值
原子值用于指定没有子代或父代的节点。例如: 在上述XML文档中,以下是原子值:
Chetan Bhagat
" en"
节点的关系
父节点
每个元素和属性都有一个父级,该父级是
请参见以下示例:
在此示例中,book元素是标题,作者,年份和价格。
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
子节点
子节点可以有零个,一个或多个子节点。在此示例中,标题,作者,年份和价格元素都是book元素的子元素。
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
兄弟姐妹节点
具有相同父节点的节点称为兄弟姐妹。在此示例中,标题,作者,年份和价格元素均为同级。
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
祖先
节点的父级或父级父级被指定为祖先。在此示例中,title元素的祖先是book元素和library元素。
<Library>
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
</Library>
后代
后代被指定为节点的子代或子代的子代。在此示例中,库元素的后代是书,书名,作者,年份和价格元素。
<Library>
<book>
<title lang="en">Three Mistakes of My Life</title>
<author>Chetan Bhagat</author>
<year>2008</year>
<price>110</price>
</book>
</Library>