xsl-list
[Top] [All Lists]

RE: testing for string and number in XSLT 2.0 was Re: [xsl] Test For Numeric Values?

2005-04-08 04:17:58
    <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
specification.

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+')"/>
</xsl:function>

Michael Kay
http://www.saxonica.com/



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>