我确实有一个示例XML
文件:-
<?xml version="1.0"?>
<bookstore>
<book>
<title>The Weather Pattern</title>
<author>Weather Man</author>
<price>100.00</price>
</book>
<book>
<title>Weaving Patterns</title>
<author>Weaver</author>
<price>150.00</price>
</book>
<book>
<title>Speech Pattern</title>
<author>Speaker</author>
<price>15.00</price>
</book>
<book>
<title>Writing Style</title>
<author>Writer</author>
<price>1500.00</price>
</book>
</bookstore>
我将XPATH表达式写成:
//author[starts-with(text(),'We')]/../*[local-name(.) = 'price' or local-name(.) = 'author']/text()
输出
Weather Man
100.00
Weaver
150.00
我正在寻找一个经过修改、简化的xpath表达式来获得相同的输出。不同的想法也是受欢迎的。有什么帮助吗?
这是一个经过修改的XPath表达式。它给出了相同的输出。简单在旁观者眼里。
//book[starts-with(author, "We")]/*[not(local-name(.)="price")]/text()
或者,没有local-name
,这只有在涉及不同名称空间时才有意义:
//book[starts-with(author, 'We')]/*[self::author or self::title]/text()
//book[starts-with(author, 'We')]/*[not(self::price)]/text()