Re: [xsl] What is the best way to cast integer to string in XSLT2?

Subject: Re: [xsl] What is the best way to cast integer to string in XSLT2?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sun, 11 May 2003 14:41:33 +0100
Hi Dimitre,

> With Saxon 7.5 I'm getting an error on the following:
>
>      <xsl:value-of select="concat(position(), '. ', ., '&#xA;')"/>
>
> "Type error in first argument of call to concat():
>   Required type is xs:string; supplied value has type xs:integer"
>
> 1. Why doesn't the XSLT processor perform an implicit cast from
> integer to string? This is quite natural, as any atomic type has a
> string representation and can be converted to string.

This is a good example of what I meant during my talk at XML Europe
about life getting harder for XSLT users because of the strong typing
in XPath 2.0. XPath 2.0 allows implicit casting:

 1. from xdt:untypedAtomic (the type of untyped nodes) to any atomic
    type

 2. from any atomic type to its base type (up the atomic type
    hierarchy)

 3. from xs:decimal (and its subtypes) to xs:float or xs:double and
    from xs:float to xs:double

As you say, it is possible to cast values of most types into a string
(exceptions are xs:QName and xs:NOTATION), but in terms of the type
hierarchy used by XPath 2.0, it is not the case that every type is
derived from xs:string. (I guess you're aware of the recent XML-Dev
discussion on this topic.) So you have to perform the casting
explicitly.

> 2. My current solution is to use
>
>   string(position())
>
> Is this the best/recommended solution?

Yes. For atomic values like the integer returned by position(), it
returns the same as xs:string(position()); the latter is just longer
and requires an extra namespace declaration.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread