xsl-list
[Top] [All Lists]

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

2003-05-11 14:09:00
Hi Dimitre,

Do any examples exist when such implicit casting would be
dangerous/counter-intuitive? If not, then it would be very useful to
allow implicit casting to string.

I agree, but then I think that it would be useful to allow implicit
casting to a required type, whatever it is, if such a cast is
permitted. Those that support strong typing in XPath 2.0, on the other
hand, believe that it's useful to be told that you can't use an
integer where a string is expected.

There are lots of cases where there *is* implicit casting (of
xdt:untypedAtomic values) in XPath 2.0 and that casting is
counter-intuitive (to XPath 1.0 users). For example:

  @height > @weight

casts both attributes to strings and compares them lexicographically,
rather than doing a numeric comparison as in XPath 1.0, while:

  current-dateTime() + @delay

(where @delay is "PT30M", for example) will give you a type error
because the delay attribute is cast to an xs:double (even though you
can't actually add an xs:double to an xs:dateTime) and "PT30M" isn't a
valid double value.

Similarly:

  24 idiv (@indent + 3)

will give you a type error because the indent attribute is cast to an
xs:double, which means the result of (@indent + 3) is an xs:double,
which isn't a valid type for an operand to idiv.

Which I guess goes to show that implicit casting coupled with
polymorphic operators will always give some weird results.

In cases like the current one I think such "conversion constructors"
should be specified in the XPath data model. This makes writing and
understanding a program much more easier.

The rules for casting between types are specified in the XPath/XQuery
Functions & Operators document (http://www.w3.org/TR/xpath-functions).
As I said above, personally I don't see why these rules can't be used
for implicit casting, but this seems to go against the strong typing
ethos of XPath 2.0.

Cheers,

Jeni

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


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