有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何获取节点的字符串值?

XPath string(/ROOT/Products/UnitPrice)在dom4j&;这个NET运行时。但在撒克逊,它引发了一个例外:

net.sf.saxon.s9api.SaxonApiException: A sequence of more than one item is not allowed as the first argument of string() (<UnitPrice/>, <UnitPrice/>, ...) 

这是怎么回事?为什么这不好


共 (3) 个答案

  1. # 1 楼答案

    问题是:/ROOT/Products/UnitPrice可能返回多个结果,XPath 2.0string函数不接受多个参数(see here

    Saxon兼容XPath 2.0。要解决您的问题,可以编写以下XPath表达式:

    for $price in /ROOT/Products/UnitPrice return string($price)
    

    然后必须迭代结果(XdmValue对象)

  2. # 2 楼答案

    Saxon需要一个节点作为输入

    那个。NET实现是不同的;它只考虑了第一个问题:

    The string() function converts a node-set to a string by returning the string value of the first node in the node-set, which in some instances may yield unexpected results.

    MSDN

  3. # 3 楼答案

    如果使用s9api接口,可以调用

    XPathCompiler.setBackwardsCompatible(true);

    使XPath表达式在XPath 1.0兼容模式下运行。这并不是完全复制XPath 1.0行为的所有方面,但它将处理XPath 1.0和2.0之间发生变化的大部分事情

    在2.0中引入的不兼容性通常是因为它们影响了1.0中用户错误的常见来源。最好不要依赖string()等函数对输入序列执行的隐式截断;这是许多应用程序错误的原因

    ==以后==

    我们试图在Saxon HE 9.8中删除1.0兼容模式,认为10年后很少有人会仍然依赖它。不幸的是,这几个人大惊小怪,我们决定后退。但我刚刚看到,在HE 9.8中,setBackwardsCompatible()方法会抛出一个错误,表示它不受支持。试试看:

    XPathCompiler.getUnderlyingStaticContext().setBackwardsCompatibilityMode(true);