xpath 多标签选择

发布于 2024-11-18 02:26:08 字数 1658 浏览 2 评论 0原文

对于给定的 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()); 
   }
}

}

任何人都可以帮助我吗?

多谢!!!

For the given XML how can I select c,d,g,h (which will be child tags of b not in j) using xpath?

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>

I thought of using following to grab the result but it doesn't give me g,h values

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

java code I used

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()); 
   }
}

}

Can anyone help me with this?

Thanks a lot!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一杯敬自由 2024-11-25 02:26:08

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

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

如果您想指定从根开始的完整路径,请使用此 xpath:

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

或者如果您想要 b 内的所有 c、d、g 或 h :

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

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

Use this xpath if you want to select all c, d, g, h nodes:

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

Use this, if you want to specify the full path from the root:

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

Or if you want all c, d, g or h, which are within b:

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

Also, in your code: use nodes.item(i).getTextContent() instead of GetNodeValue.

糖粟与秋泊 2024-11-25 02:26:08

使用

 //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)] | */*)

Use:

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

In case you know the structure of the XML document well and it is true that the only grand-children that //a/b can have are g and/or h, then this can be simplified to:

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

In XPath 2.0 this can be written even simpler as:

 //a/b/(*[not(self::e or self::f)] | */*)
蒗幽 2024-11-25 02:26:08

如何使用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']"

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

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

)]"

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

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

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

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

how can I select c,d,g,h (which will be child tags of b not in j) using xpath?

XPath 2.0

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

To stay with your initial location path, XPath 1.0 should be:

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

Or, following your usage of axis:

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

By appending to the location paths above text(), you will get the text node from each related tag.

PS: The solution given by @fiver sould be changed to /a/b/c|/a/b/d|/a/b/f/g|/a/b/f/h.

)]"

To stay with your initial location path, XPath 1.0 should be:

Or, following your usage of axis:

By appending to the location paths above text(), you will get the text node from each related tag.

PS: The solution given by @fiver sould be changed to /a/b/c|/a/b/d|/a/b/f/g|/a/b/f/h.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文