ChatGPT解决这个技术问题 Extra ChatGPT

获取子节点包含属性的节点

假设我有以下 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>

有什么提示吗?

这是什么 XPath 实现?

l
lavinio

尝试

//book[title/@lang = 'it']

内容如下:

获取至少有一个标题的所有书籍元素,该标题的属性 lang 值为“it”

至少有一个标题,其属性 lang 的值为“it”

它的属性 lang 的值为“it”

值为“它”

您可能会发现 this 很有帮助 - 这是 Ronald Bourret 的一篇题为 "XPath in Five Paragraphs" 的文章。

但老实说,//book[title[@lang='it']] 和上面的应该是等价的,除非你的 XPath 引擎有“问题”。因此,它可能是您未向我们展示的代码或示例 XML 中的某些内容——例如,您的示例是一个 XML 片段。是不是根元素有一个命名空间,而您在查询中没有计算在内?而你只告诉我们它不起作用,但你没有告诉我们你得到了什么结果。


如果 title 不是 book 的直接子代,但在更深的地方并且我们不知道确切的位置,如何做同样的事情? //book[/title/@lang = 'it'] 似乎不起作用?
马丁,你可以使用 //book[.//title/@lang = 'it']。我相信诀窍是“。”在条件的开始。
感谢您的链接,优秀的文章。我多年来一直在使用 xPath,但这确实帮助我理解了底层逻辑!
D
Divyesh Kanzariya

多年后,一个有用的选择是利用 XPath Axes (https://www.w3schools.com/xml/xpath_axes.asp)。更具体地说,您希望使用 descendants 轴。

我相信这个例子可以解决问题:

//book[descendant::title[@lang='it']]

这允许您选择包含等于“it”的语言属性值的子 title 元素(无论嵌套多深)的所有 book 元素。

我不能确定这个答案是否与 2009 年相关,因为我不能 100% 确定当时存在 XPath Axes。我可以确认的是它们今天确实存在,而且我发现它们在 XPath 导航中非常有用,我相信你也会。


3
3 revs, 2 users 71%
//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 ");*/

      }
  }
}

+1 这是一个合规性问题,并且语法会生成相同的节点集。 C# 中的类似代码也适用。
-1:张先生,我试图通过删除与问题无关的代码来帮您一个忙。它让我不会对你投反对票,我现在觉得我必须这样做。请注意,没有其他答案包含调用查询的代码。
+1:因为我不知道桑德斯先生在说什么 - 没有其他答案添加任何代码,这个答案显示了使用的代码,所以我们可以 1:验证他的方法和 2:自己执行他的测试。代码简短易读。我没有看到问题。
A
Arpit

我认为您自己的建议是正确的,但是 xml 不太有效。如果您在 <root>[Your"XML"Here]</root> 上运行 //book[title[@lang='it']],那么免费的在线 xPath 测试器(例如一个 here)将找到预期的结果。


u
user1113000

尝试使用这个 xPath 表达式:

//book/title[@lang='it']/..

那应该为您提供“it”语言中的所有书籍节点


该表达式的结果是标题节点,而不是书籍节点
那不是真的。它将返回书本节点(末尾的这两个点是针对标题节点的上节点)。