Handling empty sequence in XSLT function

2019-04-22 02:56发布

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

"An empty sequence is not allowed as the first argument of cda:isValidDate()"

Any suggestions how to handle this situation ?

标签: xml xslt
1条回答
ゆ 、 Hurt°
2楼-- · 2019-04-22 03:40

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).

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.

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>

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.

查看更多
登录 后发表回答