xpath 多标签选择
对于给定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想选择所有 c、d、g、h 节点,请使用此 xpath:
如果您想指定从根开始的完整路径,请使用此 xpath:
或者如果您想要 b 内的所有 c、d、g 或 h :
另外,在您的代码中:使用
nodes.item(i).getTextContent()
而不是 GetNodeValue。Use this xpath if you want to select all c, d, g, h nodes:
Use this, if you want to specify the full path from the root:
Or if you want all c, d, g or h, which are within b:
Also, in your code: use
nodes.item(i).getTextContent()
instead of GetNodeValue.使用:
如果您很了解 XML 文档的结构并且确实是
//a/b
的唯一孙子可以有g
和/或h
,那么这可以简化为:在 XPath 2.0 中可以写得更简单为:
Use:
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 areg
and/orh
, then this can be simplified to:In XPath 2.0 this can be written even simpler as:
XPath 2.0
保持您的初始位置路径,XPath 1.0 应该是:
或者,按照您对 axis 的使用:
通过附加到
text()
上方的位置路径,您将获得文本每个相关标签的节点。PS:@ Fiver给出的解决方案应该改为
/a/b/c|/a/b/d|/a/b/f/g|/a/b/f/h
。XPath 2.0
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
.