XPath返回错误的布尔结果

时间:2015-02-09 15:55:31

标签: xml xpath saxon

我试图通过Saxon运行XPath,我有以下文档:

<ServiceContext>
    <userName>admin</userName>
</ServiceContext>

然后我将其加载到名为doc的XdmItem中,然后运行以下查询

XPathExecutable expr = compiler.compile("/ServiceContext/userName/text()='admin'")
final XPathSelector selector = expr.load();
selector.setContextItem(doc);
final boolean value = selector.effectiveBooleanValue();

它应该返回true但由于某种原因它返回false,有人知道这里有什么问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不了解撒克逊人,但使用现成的javax.xml课程,它按预期工作。

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();

Document d = builder.parse(new ByteArrayInputStream("<ServiceContext>\n<userName>admin</userName>\n</ServiceContext>".getBytes()));
String value = xpath.evaluate("/ServiceContext/userName/text()='admin'", d);

value包含true。 也许升级您的Saxon或切换到javax.xml