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

时间:2009-09-22 00:56:49

标签: 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>

我想做一个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>

任何提示?提前谢谢。

5 个答案:

答案 0 :(得分:151)

尝试

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

这是:

  • 获取所有book元素
    • 至少有一个title
      • ,其属性为lang
        • ,其值为"it"

您可能会发现this有用 - 这是Ronald Bourret撰写的一篇名为"XPath in Five Paragraphs"的文章。

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

答案 1 :(得分:43)

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

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

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

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

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

答案 2 :(得分:9)

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

      }
  }
}

答案 3 :(得分:3)

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

答案 4 :(得分:0)

尝试使用此xPath表达式:

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

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