0

我确实有一个示例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 表达式来获得相同的输出。也欢迎不同的想法。有帮助吗?

4

1 回答 1

1

这是一个修改后的 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()
于 2013-08-28T08:48:33.510 回答