[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Handling type conversion errors


Subject: [xsl] Handling type conversion errors
From: "Martinez, Brian" <brian.martinez@xxxxxxxxxxx>
Date: Tue, 14 Oct 2003 14:02:48 -0600

Is there a standard way of handling type conversion errors in XPath 1.0/XSLT
1.0?  I understand that *most* data types can be implicitly cast to another
type, except to node-sets or RTFs.  But when a cast error *does* occur, how
should it be reported?

Consider the following stylesheet:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="iso-8859-1"/>
  
  <xsl:template match="/">
    <vendors>
      <xsl:call-template name="build-vendor-list">
        <xsl:with-param name="preferred-vendors"/>
        <xsl:with-param name="nonpreferred-vendors"/>
      </xsl:call-template>
    </vendors>
  </xsl:template>
  
  <xsl:template name="build-vendor-list">
    <xsl:param name="preferred-vendors"/>
    <xsl:param name="nonpreferred-vendors"/>
    <preferred>
      <xsl:for-each select="$preferred-vendors/car-vendor">
        <vendor code="{@code}"><xsl:value-of select="."/></vendor>
      </xsl:for-each>
    </preferred>
    <nonpreferred>
      <xsl:for-each select="$nonpreferred-vendors/car-vendor">
        <vendor code="{@code}"><xsl:value-of select="."/></vendor>
      </xsl:for-each>
    </nonpreferred>
  </xsl:template>
</xsl:stylesheet>

You'll note that the call to build-vendor-list contains a critical error; I
don't set the value of the parameters passed to the template, so the
for-each loops attempt to iterate over what are essentially empty strings
(yes, I actually made this silly mistake, fortunately caught in QA).

Xalan (v2.5.1d at least) throws a ClassCastException and our presentation
service spits out a half-rendered page.  When Xalan encounters other
XSLT/XPath errors, it returns a message in addition to a stack trace; here
it just seems to barf.

Saxon 6.5.2 reports "The value is not a node-set," which is a lot more
useful.

Is this implementation-dependent?  Is the expectation that our presentation
service should catch and handle the exception?  Actually, this reveals
another question: shouldn't Xalan be testing for a valid node-set instead of
attempting a type conversion, since even implicit conversions to a node-set
aren't allowed?

thanks for the insight,
b.

| brian martinez                           brian.martinez@xxxxxxxxxxx |
| lead gui programmer                                    303.357.3548 |
| cheap tickets, part of trip network                fax 303.357.3380 |
| 6560 greenwood plaza blvd., suite 400           englewood, co 80111 |
| cendant travel distribution services   http://www.cheaptickets.com/ |


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords