Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XPath返回错误的布尔结果_Xml_Xpath_Saxon - Fatal编程技术网

Xml XPath返回错误的布尔结果

Xml XPath返回错误的布尔结果,xml,xpath,saxon,Xml,Xpath,Saxon,我正在尝试通过Saxon运行XPath,我有以下文档: <ServiceContext> <userName>admin</userName> </ServiceContext> 它应该返回true,但由于某些原因返回false,有人知道这里出了什么问题吗 谢谢 我不了解Saxon,但使用现成的javax.xml类,它可以正常工作 DocumentBuilderFactory domFactory = DocumentBuilderFac

我正在尝试通过Saxon运行XPath,我有以下文档:

<ServiceContext>
    <userName>admin</userName>
</ServiceContext>
它应该返回true,但由于某些原因返回false,有人知道这里出了什么问题吗


谢谢

我不了解Saxon,但使用现成的
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);
DocumentBuilderFactory domFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=domFactory.newDocumentBuilder();
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
Document d=builder.parse(新的ByteArrayInputStream(“\nadmin\n”.getBytes());
String value=xpath.evaluate(“/ServiceContext/userName/text()='admin',d);
包含
真值

也许升级您的Saxon或切换到
javax.xml

,这应该可以工作。要查看实际值是什么(您希望是
'admin'
),可以尝试
编译器。编译(“/ServiceContext/userName/text()”
),然后打印值
最终字符串值=选择器。effectiveStringValue()
不,我看不出这里有什么问题。我怀疑这是代码中你没有展示给我们的部分。您是否尝试过从XQuery命令行执行表达式?是的,它与未显示的代码片段有关编译器设置了错误的URN
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);