假设我有以下 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”的书节点。
我的尝试看起来像这样:
//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>
有什么提示吗?
尝试
//book[title/@lang = 'it']
内容如下:
获取至少有一个标题的所有书籍元素,该标题的属性 lang 值为“it”
至少有一个标题,其属性 lang 的值为“it”
它的属性 lang 的值为“it”
值为“它”
您可能会发现 this 很有帮助 - 这是 Ronald Bourret 的一篇题为 "XPath in Five Paragraphs" 的文章。
但老实说,//book[title[@lang='it']]
和上面的应该是等价的,除非你的 XPath 引擎有“问题”。因此,它可能是您未向我们展示的代码或示例 XML 中的某些内容——例如,您的示例是一个 XML 片段。是不是根元素有一个命名空间,而您在查询中没有计算在内?而你只告诉我们它不起作用,但你没有告诉我们你得到了什么结果。
多年后,一个有用的选择是利用 XPath Axes (https://www.w3schools.com/xml/xpath_axes.asp)。更具体地说,您希望使用 descendants 轴。
我相信这个例子可以解决问题:
//book[descendant::title[@lang='it']]
这允许您选择包含等于“it”的语言属性值的子 title
元素(无论嵌套多深)的所有 book
元素。
我不能确定这个答案是否与 2009 年相关,因为我不能 100% 确定当时存在 XPath Axes。我可以确认的是它们今天确实存在,而且我发现它们在 XPath 导航中非常有用,我相信你也会。
//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 ");*/
}
}
}
我认为您自己的建议是正确的,但是 xml 不太有效。如果您在 <root>[Your"XML"Here]</root>
上运行 //book[title[@lang='it']]
,那么免费的在线 xPath 测试器(例如一个 here)将找到预期的结果。
尝试使用这个 xPath 表达式:
//book/title[@lang='it']/..
那应该为您提供“it”语言中的所有书籍节点
不定期副业成功案例分享
title
不是book
的直接子代,但在更深的地方并且我们不知道确切的位置,如何做同样的事情?//book[/title/@lang = 'it']
似乎不起作用?