Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling empty sequence in XSLT function

Tags:

xml

xslt

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 ?

like image 826
Laxmikanth Samudrala Avatar asked Aug 21 '09 04:08

Laxmikanth Samudrala


1 Answers

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.

like image 63
Robert Christie Avatar answered Sep 21 '22 18:09

Robert Christie