2014-03-13 48 views
0

我在學習XPath語法。我使用的是W3Schools的例子在這裏:在XPath中選擇過濾節點集的屬性

http://www.w3schools.com/xsl/tryit.asp?filename=try_xpath_select_pricenodes_high

..這是基於以下XML:

<?xml version="1.0" encoding="UTF-8""?> 

<bookstore> 

<book category="COOKING"> 
    <title la="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> 

</bookstore> 

的例子選擇具有價格比35.我越大的書籍標題想玩這個例子,並選擇類別名稱,而不是標題。所以,我試過這個:

/bookstore/book[price>35]/@category 

而且,正如你在該網站上自己測試它所看到的那樣,它不產生任何輸出。我錯了什麼?

謝謝你的時間。

+0

您的示例沒有任何價格> 35的書。是否是錯字? –

回答

1

查詢無誤。但是由於您正在處理屬性節點而不是元素,因此您必須調整打印結果的代碼。更改線路

document.write(nodes[i].childNodes[0].nodeValue); 
document.write(result.childNodes[0].nodeValue); 

document.write(nodes[i].nodeValue); 
document.write(result.nodeValue); 

,你會得到預期的結果。

+0

啊哈!我幾乎把我的頭髮拉出來,無法得到這樣一個簡單的例子來工作!謝謝! – BigErn77

相關問題