处理 XSLT 函数中的空序列 [英] Handling empty sequence in XSLT function
问题描述
我有一个 XSLT 函数,用于检查发送的参数是否为 YYYYMMDD 格式.在某些情况下,我没有得到该函数的任何价值,在这些情况下,SAXON 抛出以下错误
I have an XSLT function which checks whether the sent parameter is in YYYYMMDD format or not. In some conditions I am not getting any value to the function, during these conditions SAXON is throwing below error
cda:isValidDate() 的第一个参数不允许空序列"
"An empty sequence is not allowed as the first argument of cda:isValidDate()"
有什么建议可以处理这种情况吗?
Any suggestions how to handle this situation ?
推荐答案
在 XSLT 中没有 Null 值.要表示缺失值,您可以使用零长度字符串或空序列.它们不是一回事 - 空序列将从 count($x)
返回 0 但零长度字符串是包含一个 xs:string 类型的项目的序列,其字符串长度为 0 (count($x) = 1
和 string-length($x) = 0
).
In XSLT there is no Null value. To represent a missing value, you can use a zero length string or an empty sequence. They are not the same thing - an empty sequence would return 0 from count($x)
but a zero length string is a sequence containing one item of type xs:string which has a string length of 0 (count($x) = 1
and string-length($x) = 0
).
大多数标准 XPath 函数接受零长度字符串或空序列,但您的自定义函数可能不接受.
Most of the standard XPath functions accept either a zero length string or an empty sequence but your custom function may not.
如果您选择字符序列,则可能会出现问题.例如,如果您选择包含节点不存在的字符串的节点的值,您将获得一个空序列 - 但如果该节点存在且值为空,您将获得空字符串.
The problem may be occurring if you're selecting the sequence of characters. For example, if you select the value of a nodes that contains the string the node does not exist, you'll get an empty sequence - but if the node exists and the value is empty, you'll get the empty-string.
修改您选择值的方式以始终具有空字符串(或包装/更改 isValidDate
函数以接受空序列).以下函数定义将接受空序列并将其转换为零长度字符串:
Modify the way you select the value to always have the empty string (or wrap/change the isValidDate
function to accept the empty-sequence). The following function definition will accept the empty sequence and convert it to a zero length string:
<xsl:function name="cda:isValidDate" as="xs:boolean">
<xsl:param name="datestring" as="xs:string?"/>
<xsl:variable name="reallyastring" select="string($datestring)"/>
Your code
</xsl:function>
xs:string?
参数类型上的 ?
允许提供一项或不提供一项.string(...)
函数从不返回空字符串,因此会将空序列转换为零长度字符串.
The ?
on the xs:string?
param type allows one or no items to be provided. The string(...)
function never returns an empty string so will convert the empty sequence to a zero length string.
这篇关于处理 XSLT 函数中的空序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!