XPath 如何以不知道命名空间的方式识别谓词中的属性 [英] XPath how to identify an attribute in a predicate in a namespace-unaware way
问题描述
我有以下 XML 文件:
I have the following XML file:
<foo:a xmlns:foo=\"http://www.foo.com\">
<foo:b foo:bar=\"zar\">
</foo:b>
</foo:a>
要获取属性 bar 的 b 节点的值为zar"(都在正确的命名空间中),我可以使用 XPath 表达式:
To get the b nodes with an attribute bar having the value "zar" (all in the correct namespace), I can use the XPath expression:
/foo:a/foo:b[@foo:bar=\"zar\"]
(foo"正确绑定到http://www.foo.com" - 见代码最后)
(with "foo" properly bound to "http://www.foo.com" - see code at the end)
但是,当我想以不知道命名空间的方式执行相同操作时,虽然我可以通过依赖 local-name()
函数从元素中删除命名空间,但我无法将它们从属性中移除.
However, when I want to do the same in a namespace-unaware way, though I can remove the namespace from the elements by relying on the local-name()
function, I am not able to remove them from the attribute.
这是我能想到的最好的:
This is the best I can come up with:
/*[local-name()='a']/*[local-name()='b' and @foo:bar=\"zar\"]
(遗憾的是,其中 foo
用于限定 bar
属性).
(where foo
is, regrettably, used to qualify the bar
attribute).
如何在完全删除命名空间的情况下编写上述表达式?
How can I write the above expression with namespaces completely removed?
下面的代码已经在 CLASSPATH 上使用和不使用 Saxon-HE-9.4.jar 进行了测试,并产生了正确的结果,但我无法从第二个中获取 'foo' 命名空间前缀XPath 表达式!
Below code has been tested both with and without Saxon-HE-9.4.jar on the CLASSPATH and produces correct results, but I can't get the 'foo' namespace prefix out of the second XPath expression!
import java.io.*;
import java.util.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.namespace.NamespaceContext;
public class FooMain {
public static void main(String args[]) throws Exception {
String xmlSample = "<foo:a xmlns:foo=\"http://www.foo.com\"><foo:b foo:bar=\"zar\"></foo:b></foo:a>";
XPath xpath = namespaceAwareXpath("foo", "http://www.foo.com");
{
System.out.printf("[NS-aware ] Number of 'b' nodes with foo:bar attribute having the value \"zar\" is: %d\n",
((NodeList) xpath.compile("/foo:a/foo:b[@foo:bar=\"zar\"]").evaluate(stringToXML(xmlSample, true),
XPathConstants.NODESET)).getLength());
}
{
System.out.printf("[NS-aware but using local-name() ] Number of 'b' nodes with foo:bar attribute having the value \"zar\" is: %d\n",
((NodeList) xpath.compile("/*[local-name()='a']/*[local-name()='b' and @foo:bar=\"zar\"]").evaluate(stringToXML(xmlSample, true),
XPathConstants.NODESET)).getLength());
}
}
public static XPath namespaceAwareXpath(final String prefix, final String nsURI) {
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
NamespaceContext ctx = new NamespaceContext() {
@Override
public String getNamespaceURI(String aPrefix) {
if (aPrefix.equals(prefix))
return nsURI;
else
return null;
}
@Override
public Iterator getPrefixes(String val) {
throw new UnsupportedOperationException();
}
@Override
public String getPrefix(String uri) {
throw new UnsupportedOperationException();
}
};
xpath.setNamespaceContext(ctx);
return xpath;
}
private static Document stringToXML(String s, boolean nsAware) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(nsAware);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8")));
}
}
推荐答案
/*[local-name()='a']/*[local-name()='b' and @*[local-name() = 'bar']=\"zar\"]
这篇关于XPath 如何以不知道命名空间的方式识别谓词中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!