xsl-list
[Top] [All Lists]

Re: [xsl] "castable as" explanation

2006-08-28 08:10:49
On Monday 28 August 2006 14:48, cknell(_at_)onebox(_dot_)com 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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>