2013-02-26 65 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下的元素)。