Using the fn:error Function

Using the fn:error Function

The fn:error function makes the XPath processor display an error message and stop processing. You can pass the error message you want displayed to this function like thisthere is no return type, which XPath indicates with a return type of "none" ("none" is a special type defined in XPath 2.0, and it's not actually usable in the XPath language with XPath processorsit's just to indicate there is no return type):

 
 fn:error(  $srcval  as  item?  ) as none 

You can see an example in ch09_05.xsl (Listing 9.5). In this case, we're checking the number of a particular item we have in stock and reporting the results.

Listing 9.5 Using the fn:error Function ( ch09_05.xsl )
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:variable name="inStock" select="80" />     <xsl:template match="/">  <xsl:value-of select="if ($inStock > 0)   then concat('Number in stock: ', string($inStock))   else error('Invalid argument')"/>  </xsl:template> </xsl:stylesheet> 

Here's the result using Saxon as the example is written:

 
 <?xml version="1.0" encoding="UTF-8"?> Number in stock: 80 

However, if we change the number in stock to 20 like this:

 
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xsl:variable name="inStock" select="-20" />  <xsl:template match="/">        <xsl:value-of select="if ($inStock > 0)            then concat('Number in stock: ', string($inStock))            else error('Invalid argument')"/>     </xsl:template> </xsl:stylesheet> 

Then running Saxon on this example raises this error and makes Saxon quit:

 
 C:\Saxon>java net.sf.saxon.Transform ch09_02.xml ch09_05.xsl Error at xsl:value-of on line 7 of file:/C:/Saxon/ch09_05.xsl:   Invalid argument Transformation failed: Run-time errors were reported 

In this way, you can cause an error and make the XPath processor report that errorthe string we passed to the fn:error function is displayed by Saxon.

Constructor Functions

You can use constructor functions to create typed values in XPath 2.0. There is a constructor function for every built-in atomic type except xs:NOTATION . Here's what they look like:

 
  pref:type  (  $srcval  as xdt:anyAtomicType) as  pref:type  

Here, pref is the prefix of the type you want to create, either xs or xdt . A constructor function has the same name as the type you want to createfor example, to create an xs:double with a value of 3.1415, you can use the xs:double constructor function like this:

 
 xs:double("3.1415") 

This expression returns an xs:double value of 3.1415. Here are the legal constructor functions that are supported for the built-in types:

  • xdt:untypedAtomic( $srcval as xdt:anyAtomicType) as xdt:untypedAtomic

  • xdt:dayTimeDuration( $srcval as xdt:anyAtomicType) as xdt:dayTimeDuration

  • xdt:yearMonthDuration( $srcval as xdt:anyAtomicType) as xdt:yearMonthDuration

  • xs:anyURI( $srcval as xdt:anyAtomicType) as xs:anyURI

  • xs:QName( $srcval as xdt:anyAtomicType) as xs:QName

  • xs:base64Binary( $srcval as xdt:anyAtomicType) as xs:base64Binary

  • xs:boolean( $srcval as xdt:anyAtomicType) as xs:boolean

  • xs:byte( $srcval as xdt:anyAtomicType) as xs:byte

  • xs:date( $srcval as xdt:anyAtomicType) as (xs:date, xdt:dayTimeDuration)

  • xs:dateTime( $srcval as xdt:anyAtomicType) as (xs:dateTime, xdt:dayTimeDuration)

  • xs:decimal( $srcval as xdt:anyAtomicType) as xs:decimal

  • xs:double( $srcval as xdt:anyAtomicType) as xs:double

  • xs:duration( $srcval as xdt:anyAtomicType) as xs:duration

  • xs:ENTITY( $srcval as xdt:anyAtomicType) as xs:ENTITY

  • xs:float( $srcval as xdt:anyAtomicType) as xs:float

  • xs:gDay( $srcval as xdt:anyAtomicType) as xs:gDay

  • xs:gMonth( $srcval as xdt:anyAtomicType) as xs:gMonth

  • xs:gMonthDay( $srcval as xdt:anyAtomicType) as xs:gMonthDay

  • xs:gYear( $srcval as xdt:anyAtomicType) as xs:gYear

  • xs:gYearMonth( $srcval as xdt:anyAtomicType) as xs:gYearMonth

  • xs:hexBinary( $srcval as xdt:anyAtomicType) as xs:hexBinary

  • xs:ID( $srcval as xdt:anyAtomicType) as xs:ID

  • xs:IDREF( $srcval as xdt:anyAtomicType) as xs:IDREF

  • xs:int( $srcval as xdt:anyAtomicType) as xs:int

  • xs:integer( $srcval as xdt:anyAtomicType) as xs:integer

  • xs:language( $srcval as xdt:anyAtomicType) as xs:language

  • xs:long( $srcval as xdt:anyAtomicType) as xs:long

  • xs:Name( $srcval as xdt:anyAtomicType) as xs:Name

  • xs:NCName( $srcval as xdt:anyAtomicType) as xs:NCName

  • xs:negativeInteger( $srcval as xdt:anyAtomicType) as xs:negativeInteger

  • xs:NMTOKEN( $srcval as xdt:anyAtomicType) as xs:NMTOKEN

  • xs:nonNegativeInteger( $srcval as xdt:anyAtomicType) as xs:nonNegativeInteger

  • xs:nonPositiveInteger( $srcval as xdt:anyAtomicType) as xs:nonPositiveInteger

  • xs:normalizedString( $srcval as xdt:anyAtomicType) as xs:normalizedString

  • xs:positiveInteger( $srcval as xdt:anyAtomicType) as xs:positiveInteger

  • xs:short( $srcval as xdt:anyAtomicType) as xs:short

  • xs:string( $srcval as xdt:anyAtomicType) as xs:string

  • xs:time( $srcval as xdt:anyAtomicType) as (xs:time, xdt:dayTimeDuration)

  • xs:token( $srcval as xdt:anyAtomicType) as xs:token

  • xs:unsignedByte( $srcval as xdt:anyAtomicType) as xs:unsignedByte

  • xs:unsignedInt( $srcval as xdt:anyAtomicType) as xs:unsignedInt

  • xs:unsignedLong( $srcval as xdt:anyAtomicType) as xs:unsignedLong

  • xs:unsignedShort( $srcval as xdt:anyAtomicType) as xs:unsignedShort

We saw an example in Chapter 7, where we created an xs:date value like this:

 
[View full width]
 
[View full width]
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns graphics/ccc.gif :xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <xsl:value-of select="xs:date('2004-09-02')"/> </xsl:template> </xsl:stylesheet>

You can also have constructor functions for user -defined types if you derive them by restriction from primitive types. For example, in Chapter 7, we declared a derived type by restriction from the xs:string type named StateAbbreviation like this, restricting it to two-character strings like AZ or CA:

 
 <simpleType name='xdat:StateAbbreviation'>     <restriction base='xs:string'>       <pattern value='[A-Z]{2}'/>     </restriction> </simpleType> 

After you declare a user-defined type like this, a constructor function is made available to users:

 
 xdat:StateAbbreviation(  $srcval  as xdt:anyAtomicType) as xdat:StateAbbreviation 


XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net