假设我有以下XML:
<book category="CLASSICS">
<title lang="it">Purgatorio</title>
<author>Dante Alighieri</author>
<year>1308</year>
<price>30.00</price>
</book>
<book category="CLASSICS">
<title lang="it">Inferno</title>
<author>Dante Alighieri</author>
<year>1308</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>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
我想执行一个xpath,它将返回所有具有语言属性为"it“的title节点的book节点。
我的尝试看起来像这样:
//book[title[@lang='it']]
但这并不管用。我希望能找回节点:
<book category="CLASSICS">
<title lang="it">Purgatorio</title>
<author>Dante Alighieri</author>
<year>1308</year>
<price>30.00</price>
</book>
<book category="CLASSICS">
<title lang="it">Inferno</title>
<author>Dante Alighieri</author>
<year>1308</year>
<price>30.00</price>
</book>
有什么提示吗?
发布于 2009-09-22 01:52:52
//book[title[@lang='it']]
实际上相当于
//book[title/@lang = 'it']
我用vtd-xml试过了,两个表达式输出的结果是一样的…您使用的是什么xpath处理引擎?我猜它有一致性问题,下面是代码
import com.ximpleware.*;
public class test1 {
public static void main(String[] s) throws Exception{
VTDGen vg = new VTDGen();
if (vg.parseFile("c:/books.xml", true)){
VTDNav vn = vg.getNav();
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("//book[title[@lang='it']]");
//ap.selectXPath("//book[title/@lang='it']");
int i;
while((i=ap.evalXPath())!=-1){
System.out.println("index ==>"+i);
}
/*if (vn.endsWith(i, "< test")){
System.out.println(" good ");
}else
System.out.println(" bad ");*/
}
}
}
发布于 2012-12-21 18:46:37
我认为您自己的建议是正确的,但是xml并不完全有效。如果您在<root>[Your"XML"Here]</root>
上运行//book[title[@lang='it']]
,那么one here等免费的在线xPath测试器将会找到预期的结果。
发布于 2011-12-23 15:33:54
尝试使用此xPath表达式:
//book/title[@lang='it']/..
这将为您提供"it“语言中的所有图书节点
https://stackoverflow.com/questions/1457638
复制相似问题