If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Wednesday, December 19, 2012

Handling Nil values in XSLT / XQuery

Today I had to generate another batch of DITA files but I discovered that some failed. So the problem was that some dates were nillable and I was formatting the dates so I ran into an exception. So you can in fact pretty easily check if that element has a nil value.

<xsl:variable name="now" select="current-date()"/>
<xsl:variable name="date-format" select="'[Y0001]-[M01]-[D01]T00:00:00'"/>  

<created date="{if (not($productInfo/InitialWebPublicationDate/@xsi:nil='true')) then format-date($productInfo/InitialWebPublicationDate, $date-format) else (format-date($now, $date-format))}"/>

As Ryan Dew commented there is a shorter way like below:
<created date="{if (not(nilled($productInfo/InitialWebPublicationDate))) then format-date($productInfo/InitialWebPublicationDate, $date-format) else (format-date($now, $date-format))}"/>


  1. You might want to take a look at fn:nilled. http://www.w3.org/TR/xpath-functions/#func-nilled Keep up the posts. It is always great to see what other's are doing.

  2. And again I thank you for any useful commments ;-)