如何获取节点的字符串值?
How do I get the string value of a node?
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/>, ...)
这是怎么回事?为什么这样不行?
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
问题是:/ROOT/Products/UnitPrice
可能 return 不止一个结果,而 XPath 2.0 string
函数不接受不止一个参数 (see here)。
Saxon 与 XPath 2.0 兼容。要解决您的问题,您可以编写此 XPath 表达式:
for $price in /ROOT/Products/UnitPrice return string($price)
然后您将不得不迭代结果(XdmValue 对象)。
如果你使用的是s9api接口,可以调用
XPathCompiler.setBackwardsCompatible(true);
在 XPath 1.0 兼容模式下创建 XPath 表达式运行。这并没有完全复制 XPath 1.0 行为的所有方面,但它将处理 XPath 1.0 和 2.0 之间发生变化的大部分内容。
2.0 中引入的不兼容性通常是因为它们影响的区域是 1.0 中用户错误的常见来源。最好不要依赖像 string()
这样的函数执行的输入序列的隐式 t运行 阳离子;这是许多应用程序错误的原因。
==稍后==
我们试图在 Saxon-HE 9.8 中删除 1.0 兼容模式,认为 10 年后很少有人仍然依赖它。不幸的是,那几个人大惊小怪,我们决定原路返回。但我刚刚看到,在 HE 9.8 中,setBackwardsCompatible() 方法会抛出一个错误,说它不受支持。改为尝试:
XPathCompiler.getUnderlyingStaticContext().setBackwardsCompatibilityMode(true);
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/>, ...)
这是怎么回事?为什么这样不行?
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
问题是:/ROOT/Products/UnitPrice
可能 return 不止一个结果,而 XPath 2.0 string
函数不接受不止一个参数 (see here)。
Saxon 与 XPath 2.0 兼容。要解决您的问题,您可以编写此 XPath 表达式:
for $price in /ROOT/Products/UnitPrice return string($price)
然后您将不得不迭代结果(XdmValue 对象)。
如果你使用的是s9api接口,可以调用
XPathCompiler.setBackwardsCompatible(true);
在 XPath 1.0 兼容模式下创建 XPath 表达式运行。这并没有完全复制 XPath 1.0 行为的所有方面,但它将处理 XPath 1.0 和 2.0 之间发生变化的大部分内容。
2.0 中引入的不兼容性通常是因为它们影响的区域是 1.0 中用户错误的常见来源。最好不要依赖像 string()
这样的函数执行的输入序列的隐式 t运行 阳离子;这是许多应用程序错误的原因。
==稍后==
我们试图在 Saxon-HE 9.8 中删除 1.0 兼容模式,认为 10 年后很少有人仍然依赖它。不幸的是,那几个人大惊小怪,我们决定原路返回。但我刚刚看到,在 HE 9.8 中,setBackwardsCompatible() 方法会抛出一个错误,说它不受支持。改为尝试:
XPathCompiler.getUnderlyingStaticContext().setBackwardsCompatibilityMode(true);