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

106 xml xpath

假设我有以下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>
Run Code Online (Sandbox Code Playgroud)

我想做一个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>
Run Code Online (Sandbox Code Playgroud)

任何提示?提前致谢.

lav*_*nio 161

尝试

//book[title/@lang = 'it']
Run Code Online (Sandbox Code Playgroud)

这是:

  • 得到所有book元素
    • 至少有一个 title
      • 它有一个属性 lang
        • 价值为 "it"

你可能会觉得很有用 - 这是一篇由Ronald Bourret 撰写的题为"五段中的XPath"的文章.

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

  • 如果`title`不是`book`的直接孩子,怎么做同样的事情,但更深的地方,我们不知道到底在哪里?`// book [/ title/@ lang ='it']`似乎不起作用? (4认同)
  • 马丁,你可以使用//book[.//title/@lang ='it'].我相信诀窍是"." 在条件的开头. (4认同)

wes*_*ell 52

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

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

//book[descendant::title[@lang='it']]
Run Code Online (Sandbox Code Playgroud)

这允许您选择book包含子title元素的所有元素(无论嵌套的深度如何),其中包含等于'it'的语言属性值.

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


vtd*_*hor 10

//book[title[@lang='it']]
Run Code Online (Sandbox Code Playgroud)

实际上相当于

 //book[title/@lang = 'it']
Run Code Online (Sandbox Code Playgroud)

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

      }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • +1:因为我无法弄清楚桑德斯先生在谈论什么 - 没有其他答案添加任何代码,这个答案显示了所使用的代码,因此我们可以1:验证他的方法,2:自己进行测试.代码简短易读.我没有看到问题. (6认同)