被解析的代码如下:
[b]1.选取title为Everyday Italian,并且title的lang为en的book节点的price.[/b]
//book[node()[@lang=""en""]/text() = ""Everyday Italian""]/price/text()
运行结果:30.00
小结:1.node()[@lang=""en""]方括号中直接取属性指的取node()的lang属性
[b]2.选取bookstore下面名称为Everyday Italian的book[/b]
//bookstore/node()[*/text() = ""Everyday Italian"" ]
运行结果:
小结:[b]方括号如果不跟属性而是跟节点或者node(),*等,则表示取得当前node()的下一层节点。[/b]如:
node()[*/text() = ""Everyday Italian"" ]表示取得node()下的下一层所有节点,该节点的text()为Everyday Italian的节点。
[b]3.选取作者名不为Everyday Italian,也不为Harry Potter的book的作者名称.[/b]
/bookstore/book[title/text() != ""Everyday Italian"" and title/text() != ""Harry Potter""]/author/text()
运行结果:
x-path学习文档:[url]http://www.w3school.com.cn/xpath/index.asp[/url]
x-path在线测试路径:[url]http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_cdnodes_first[/url]
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
[b]1.选取title为Everyday Italian,并且title的lang为en的book节点的price.[/b]
//book[node()[@lang=""en""]/text() = ""Everyday Italian""]/price/text()
运行结果:30.00
小结:1.node()[@lang=""en""]方括号中直接取属性指的取node()的lang属性
[b]2.选取bookstore下面名称为Everyday Italian的book[/b]
//bookstore/node()[*/text() = ""Everyday Italian"" ]
运行结果:
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
小结:[b]方括号如果不跟属性而是跟节点或者node(),*等,则表示取得当前node()的下一层节点。[/b]如:
node()[*/text() = ""Everyday Italian"" ]表示取得node()下的下一层所有节点,该节点的text()为Everyday Italian的节点。
[b]3.选取作者名不为Everyday Italian,也不为Harry Potter的book的作者名称.[/b]
/bookstore/book[title/text() != ""Everyday Italian"" and title/text() != ""Harry Potter""]/author/text()
运行结果:
James McGovern
Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan
Erik T. Ray
x-path学习文档:[url]http://www.w3school.com.cn/xpath/index.asp[/url]
x-path在线测试路径:[url]http://www.w3school.com.cn/tiy/t.asp?f=xpth_select_cdnodes_first[/url]