Deal with empty DateTime in XSD and XSLT

In this specific case I received data from an AS400 system. In the request body are metadata with a field DATIME. An empty DATIME can be received from AS400, but the output of the XSLT must always have a valid datetime, even if not specified.

First let’s look at the correct XSD. Remember: DateTime can be filled or an empty string:

<xs:element name="Meta">
	<xs:element name="DATIME" type="emptyDate"/>

<xs:simpleType name="emptyDate">
	<xs:union memberTypes="xs:dateTime">
		<xs:restriction base="xs:string">
			<xs:enumeration value=""/>

Next the XSLT that sets Enqueued_Time to either the input datetime or the current datetime if empty:

	<xsl:when test="MSP_REQUEST_BODY/Meta/DATIME/text() != ''">
		<xsl:value-of select="MSP_REQUEST_BODY/Meta/DATIME/text()"/>
		<xsl:value-of select="userCSharp:DateNow()"/>

<msxsl:script language="C#" implements-prefix="userCSharp">
public string DateNow() 
	return(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture));