[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: testing for string and number in XSLT 2.0 was Re:

Subject: RE: testing for string and number in XSLT 2.0 was Re: Test For Numeric Values?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 8 Apr 2005 12:17:58 +0100
xsl isnumber
>     <xsl:function name="type:isnumber">
>         <xsl:param name="select"/>
>         <xsl:choose>
>             <xsl:when test="$select">
>                 <xsl:analyze-string select="$select" regex="[\d]+"
> flags="m">
>                     <xsl:matching-substring>
>                         <xsl:value-of select="true()"/>
>                     </xsl:matching-substring>
>                     <xsl:non-matching-substring>
>                         <xsl:value-of select="false()"/>      
>                     </xsl:non-matching-substring>
>                 </xsl:analyze-string>                  
>             </xsl:when>   
>             <xsl:otherwise>
>                 NaN
>             </xsl:otherwise>
>         </xsl:choose>
>     </xsl:function>

It really helps to declare the type of your function parameters and result.
There seem to be three possible results here: a text node holding the string
"true", a text node holding the string "false", and a text node holding the
string "NaN" surrounded by a lot of whitespace. Is that really what you
wanted? Moreover, you are going to get the value "true" or "false" whenever
the effective boolean value of the argument is true, and the value
"...NaN..." whenever it is false - which seems a pretty bizarre

The xsl:analyze-string construct could be written more simply as

<xsl:value-of select="matches($select, '[\d]+')"/>

> this is fine and dandy in a basic XSLT 2.0 processor, and potentiall
> could be extended, though I find it uncomfortable that xsl:function
> automatically casts my params as xs:string when there is no 
> declaration,

It doesn't. If you don't declare the types of your parameters then they
actual values supplied in the function call are passed through unchanged.

> so doing something like the following (in an XSLT 2.0 
> processor that is
> schema aware).
>         Test X (xs:integer) matching string
>         using istype:<xsl:value-of select="type:istype($x,'number')"/>
>         using isnumber:<xsl:value-of select="type:isnumber($x)"/>
>         using isstring:<xsl:value-of select="type:isstring($x)"/>

This doesn't require a schema-aware processor, you aren't doing anything
here that's not available in a basic processor
> will through out an error, I guess I could employ the 
> use-when attribute
> to check for schema conformance (does this exist as a system 
> property?).

Yes it does but you don't need it here
> btw, does anyone else find it weird that xsl:function doesnt have the
> ability to prescribe a return type ?...I wanted to return a boolean
> type...

Then (a) declare the type

<xsl:function name="x" as="xs:boolean">

and (b) return a value of that type

<xsl:sequence select="matches($arg, '\d+')"/>

In fact, I think the function you want is:

<xsl:function name="type:is-number" as="xs:boolean">
  <xsl:param name="arg" as="xs:string"/>
  <xsl:sequence select="matches($arg, '\d+')"/>

Michael Kay

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.