Altova Mailing List Archives


Re: [xsl] "castable as" explanation

From: Frans Englich <frans.englich@--------->
To:
Date: 8/28/2006 3:11:00 PM
On Monday 28 August 2006 14:48, cknell@xxxxxxxxxx wrote:
> I'm using Saxon 8.7.1J.
>
> I have output from a database in the form of an XML document that I
> transform using XSLT. As an expediant, the persons in responsible positions
> decided to encode data in a text field in order to accomplish a goal rather
> than wait until the application and database are upgraded.
>
> Some records will contain this "packed" information and others will not. I
> need to determine if an element in the output XML document contains the
> packed information or does not, and process each type differently.
>
> One of the data items packed into this text field is a date. I have
> substringed the text so as to construct a date (YYYY-MM-DD) if there is
> indeed a date encoded in the field.
>
> I thought to use "castable as" in order to distinguish between those fields
> containing the packed information and those that do not. Here the relevant
> section of my stylesheet:
>
> <xsl:variable name="is-date" select="if(DATE_PACKED_XSD castable as
> xs:date) then '1' else('0')" />
>
> <xsl:choose>
>   <xsl:when test="$is-date = '1'">
>     ... some processing here
>   </xsl:when>
>   <xsl:otherwise>
>     ... some different processing here
>   </xsl:otherwise>
> </xsl:choose>

If a variable is treated as a boolean value, let it be a boolean value. That 
is:

<xsl:variable as="xs:boolean" name="is-date" select="(DATE_PACKED_XSD castable 
as xs:date"/>

<xsl:choose>
 <xsl:when test="$is-date">
	....

The 'as="xs:boolean"' is not strictly necessary, but it makes your code safer 
and more readable, some would say.

> What I expected to happen was that when the stylesheet processed a
> DATE_PACKED_XSD element that could not be cast as a date ($is-date = 0),
> processing would pass to the <xsl:otherwise> branch.
>
> What did happen was this:
>
> stylesheet.xslt:423: Fatal Error! Invalid date "208 2-01-1 2". Non-numeric
> component
>
> What am I misapprehending about "castable as"?

Nothing about 'castable as', but "208 2-01-1 2" simply isn't a valid date. A 
valid text representation for a date is for example "2082-01-12". If you need 
to read dates in some custom format, you will need to text process those 
strings first such that they are valid xs:dateS. Here's the detailed 
description of xs:date:

http://www.w3.org/TR/xmlschema-2/datatypes.html#date


Cheers,

		Frans

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.