2013-08-28 35 views
0

我有一個樣本XML文件: -需要優化的XPath表達式,以獲得所需的節點集

<?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表達式來獲得相同的輸出。不同的想法也是受歡迎的。任何幫助?

+0

任何一個可以編輯這個職位有語法高亮效果好嗎? –

回答

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() 
+0

WoowW ...很酷..爲什麼我沒有這樣想? :)仍然* + 1 * ..非常感謝你.. –

+0

任何更多的想法做相同的任務*先生*? –

+0

@Babai:查看更新。 – choroba