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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1457638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:45:13  来源:igfitidea点击:

Get nodes where child node contains an attribute

xmlxpath

提问by lavinio

Suppose I have the following XML:

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

I would like to do an xpath that gets back all book nodes that have a title node with a language attribute of "it".

我想做一个 xpath 来取回所有具有“it”语言属性的标题节点的书籍节点。

My attempt looked something like this:

我的尝试看起来像这样:

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

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

But that didn't work. I expect to get back the nodes:

但这没有用。我希望取回节点:

<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>

Any hints?

任何提示?

回答by lavinio

Try

尝试

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

This reads:

这写着:

  • get all bookelements
    • that have at least one title
      • which has an attribute lang
        • with a value of "it"
  • 获取所有book元素
    • 至少有一个 title
      • 它有一个属性 lang
        • 价值为 "it"

You may find thishelpful — it's an article entitled "XPath in Five Paragraphs"by Ronald Bourret.

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

But in all honesty, //book[title[@lang='it']]and the above should be equivalent, unless your XPath engine has "issues." So it could be something in the code or sample XML that you're not showing us -- for example, your sample is an XML fragment. Could it be that the root element has a namespace, and you aren't counting for that in your query? And you only told us that it didn't work, but you didn't tell us what results you did get.

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

回答by wes.hysell

Years later, but a useful option would be to utilize XPath Axes (https://www.w3schools.com/xml/xpath_axes.asp). More specifically, you are looking to use the descendantsaxes.

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

I believe this example would do the trick:

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

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

This allows you to select all bookelements that contain a child titleelement (regardless of how deep it is nested) containing language attribute value equal to 'it'.

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

I cannot say for sure whether or not this answer is relevant to the year 2009 as I am not 100% certain that the XPath Axes existed at that time. What I can confirm is that they do exist today and I have found them to be extremely useful in XPath navigation and I am sure you will as well.

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

回答by vtd-xml-author

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

is actually equivalent to

实际上相当于

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

I tried it using vtd-xml, both expressions spit out the same result... what xpath processing engine did you use? I guess it has conformance issue Below is the code

我用 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 ");*/

      }
  }
}

回答by Joakim Byg

I would think your own suggestion is correct, however the xml is not quite valid. If you are running the //book[title[@lang='it']]on <root>[Your"XML"Here]</root>then the free online xPath testers such as one herewill find the expected result.

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

回答by user1113000

Try to use this xPath expression:

尝试使用这个 xPath 表达式:

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

That should give you all book nodes in "it" lang

这应该为您提供“it”lang中的所有书籍节点