XPath结点
在XPath中有七种结点,分别是:元素、属性、文本、名字空间、处理指令、注释、文档(根)。
一、XPath中的术语
1、结点(nodes)
XML文档被看作一个节点树,这个树的根就称为文档(根)结点。
示例:
<?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>
上例中的结点如下:
<bookstore> 根结点
<author>J K. Rowling</author> 元素结点
lang="en" 属性结点
2、原子值(Atomic values)
原子值是一个既没有父结点没有子结点的结点。
上例中的原子值如下:
J K. Rowling
"en"
3、项目(Items)
项目就是原子值或结点。
二、结点间的关系
1、双亲(Parent)
每个元素和属性都有一个双亲。下面的例子中book元素是title, author, year, price的双亲
示例:
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
2、子女(Children)
元素结点可以有一个或多个子女或一个都没有。
在上面的例子中title, author, year, price元素是book元素的子女。
3、同科(Siblings)
有相同双亲的结点间称为同科。上面例子中title, author, year, price元素是同科。
4、祖先(Ancestors)
一个结点的双亲、双亲的双亲等等都称为祖先。
示例:
<bookstore>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
这个例子中title元素的祖先是book元素和bookstore元素。
5、子孙(Descendants)
一个结点的子女,子女的子女都称为子孙。上例中bookstore元素的子孙是title, author, year, price
元素。
评论