2013-02-26 67 views
3

嗨,我試着弄清楚這個功能是什麼,只是展示當然是「CC130」的所有書籍?尋找「CC130」的XPath功能

我有這個「/ bookcollection /項目/項目/課程/課程」,它顯示每個過程可能嗎?

 <?xml version="1.0" encoding="utf-8"?> 
    <bookcollection> 
    <item id="59113"> 
    <title>Computer science : a modern introduction /</title> 
    <isbn>0131659456</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/59113</url> 
    <borrowedcount>29</borrowedcount> 
    <courses> 
     <course>CC100</course> 
     <course>CC130</course> 
     <course>CX290</course> 
    </courses> 
    </item> 
    <item id="59118"> 
    <title>Computer networks : protocols, standards, and interfaces /</title> 
    <isbn>0131660918</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/59118</url> 
    <borrowedcount>19</borrowedcount> 
    <courses> 
     <course>CC100</course> 
    </courses> 
    </item> 
    <item id="59131"> 
    <title>Computer-based instruction : methods and development /</title> 
    <isbn>0131685929</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/59131</url> 
    <borrowedcount>43</borrowedcount> 
    <courses> 
     <course>CC100</course> 
     <course>CC300</course> 
    </courses> 
    </item> 
    <item id="59176"> 
    <title>Computer networks : protocols, standards, and interfaces /</title> 
    <isbn>0131756052</isbn> 
    <url>http://library.hud.ac.uk/catlink/bib/59176</url> 
    <borrowedcount>10</borrowedcount> 
    <courses> 
     <course>CC100</course> 
    </courses> 
    </item> 
    </bookcollection> 

回答

1

我認爲你正在尋找:

//item[descendant::course/text() = 'CC300'] 

工作例如:http://chris.photobooks.com/xml/default.htm?state=CF

+0

這個工作非常感謝你 – 2013-02-26 14:22:27

+0

沒問題。歡迎來到堆棧溢出! – Kobi 2013-02-26 14:24:07

+0

快速問題什麼功能是隻顯示一個項目ID說我要搜索項目ID 59131? – 2013-02-26 14:39:31

1

你需要一個謂詞

/bookcollection/item[courses/course = 'CC130'] 

這將匹配所有item元素bookcollection至少有一個courses孩子包含至少一個course孩子,其值爲CC130。當XML結構是經常這樣它通常更有效,明確闡明瞭所有的步驟,而不是使用//descendant::(例如//item[.//course = 'CC130']也將工作,但將涉及各級元素命名item搜索整棵樹 - 但你知道,唯一有趣的item元素是那些直接在bookcollection下的元素)。