【问题标题】:xpath multiple tag selectxpath 多标签选择
【发布时间】:2011-09-25 17:22:15
【问题描述】:

对于给定的 XML,我如何使用 xpath 选择 c、d、g、h(这将是 b 的子标签,不在 j 中)?

XML

<a>
 <b>
  <c>select me</c>
  <d>select me</d>
  <e>do not select me</e>
  <f>
    <g>select me</g>
    <h>select me</h>
  </f>
 </b>

 <j>
  <c>select me</c>
  <d>select me</d>
  <e>do not select me</e>
  <f>
    <g>select me</g>
    <h>select me</h>
  </f>
 </j>
</a>

我想使用以下来获取结果,但它没有给我 g,h 值

xpath.compile("//a/b/*[self::c or self::d or self::f/text()");

我使用的java代码

import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;

 public class XPathDemo {

   public static void main(String[] args) 
   throws ParserConfigurationException,SAXException,IOException,PathExpressionException {

   DocumentBuilderFactory domFactory = 
   DocumentBuilderFactory.newInstance();
   domFactory.setNamespaceAware(true); 
   DocumentBuilder builder = domFactory.newDocumentBuilder();
   Document doc = builder.parse("test.xml");
   XPath xpath = XPathFactory.newInstance().newXPath();

   XPathExpression expr = xpath.compile("//a/b/*[self::c or self::d or self::f]/text()");

  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue()); 
   }
}

}

谁能帮我解决这个问题?

非常感谢!!!

【问题讨论】:

    标签: java xml xpath xml-parsing


    【解决方案1】:

    使用:

     //a/b/*[not(self::e or self::f)]
    |
     //a/b/*/*[self::g or self::h]
    

    如果您非常了解 XML 文档的结构,并且确实 //a/b 可以拥有的唯一孙子是 g 和/或 h,那么这个可以简化为:

     //a/b/*[not(self::e or self::f)]
    |
     //a/b/*/*
    

    在 XPath 2.0 中,这可以更简单地写成:

     //a/b/(*[not(self::e or self::f)] | */*)
    

    【讨论】:

    • 非常感谢 Dimitre 这种方式更简单... :)
    • 我的代表终于来了。 15 不见了:P! +1 反正你是真的。
    • @Paithra:别担心!那是代表;-)
    【解决方案2】:

    如果要选择所有 c、d、g、h 节点,请使用此 xpath:

    "//c|//d|//g|//h"
    

    如果你想从根目录指定完整路径,请使用它:

    "/a/b/c|/a/b/d|/a/b/f/g|/a/b/f/h"
    

    或者如果你想要所有 c、d、g 或 h,它们都在 b 内:

    "//b//c|//b//d|//b//g|//b//h"
    

    另外,在您的代码中:使用 nodes.item(i).getTextContent() 而不是 GetNodeValue。

    【讨论】:

    • 嗨,感谢您的帮助,我编辑了问题,我只需要 b 中的标签...我尝试使用您的查询,但出现问题,它没有给出任何结果...跨度>
    • 我不是 java 专家——你使用的是什么类——你能发布一个可以编译的代码吗?
    • "请注意,您不应该在 xpath 中包含 a,因为 a 是根。" 这个断言是与 java 相关的断言吗?因为在 XPath 中这是完全错误的。根是/。
    • 嗯,其实我也是这么想的。我不是 java 专家,我在 C# 中对此进行了测试,当我使用 /a 时它不起作用 - 所以我认为这是因为它是 root ...
    • 不,这可能与您在 C# 中使用的对象有关,该对象从 a.所以你推断不好。
    【解决方案3】:

    如何使用 xpath 选择 c、d、g、h(这将是 b 不在 j 中的子标签)?

    XPath 2.0

    "/a/b//*[matches(name(),'^c$|^d$|^g$|^h$')]"
    

    要保持初始位置路径,XPath 1.0 应该是:

    "/a/b//*[name()='c' 
      or name()='d' 
      or name()='g' 
      or name()='h']"
    

    或者,按照您对轴的使用:

     "/a/b//*[self::c 
      or self::d 
      or self::g 
      or self::h]"
    

    通过附加到text()上方的位置路径,您将从每个相关标签中获取文本节点。

    PS:@fiver 给出的解决方案应该改成/a/b/c|/a/b/d|/a/b/f/g|/a/b/f/h。

    【讨论】:

    • 非常感谢您的帮助...我使用它并解决了我的问题 XPathExpression expr = xpath.compile("//b/c | //b/d | //b/f/ g |//b/f/h/text()");
    猜你喜欢
    • 2010-10-17
    • 2023-03-28
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多