RE: [xsl] number collapsing

Subject: RE: [xsl] number collapsing
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 14 Dec 2004 15:30:35 -0000
In this example you'll be OK, because the value you're supplying is an
element, presumably an untyped element because you haven't run it past a
schema. If the element E is <E>123</E>, then (E castable as xs:integer) will
be true, and <xsl:variable name="X" as="xs:integer" select="E"/> will
succeed. But if someone (perhaps you) finds your function in three years
time and tries to call it as bib:number-condense("1", end), it will fall
over at run time with a type error. It's better to document the function's
expectations by putting "as" attributes on the xsl:param elements.

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

> -----Original Message-----
> From: Bruce D'Arcus [mailto:bdarcus@xxxxxxxxxxxxx] 
> Sent: 14 December 2004 14:48
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] number collapsing
> 
> On Dec 14, 2004, at 9:41 AM, David Carlisle wrote:
> 
> > Not sure if by "fail" you mean the <xsl:when test tests as 
> false or you
> > get a type error reported. And also not sure if by "an integer" you 
> > mean
> > a value of that type in the xpath type system or something 
> that looks
> > like an intger, eg "123".
> 
> I'm not sure either :-)
> 
> Here's an example template:
> 
>    <xsl:template match="mods:extent[@unit='page']">
>      <xsl:param name="prefix"/>
>      <xsl:param name="suffix"/>
>      <xsl:value-of select="$prefix"/>
>      <span class="pages">
>        <xsl:text> pp. </xsl:text>
>        <xsl:value-of select="mods:start"/>
>        <xsl:if test="mods:end">
>          <xsl:text>-</xsl:text>
>          <xsl:value-of 
> select="bib:number-condense(mods:start,mods:end)"/>
>        </xsl:if>
>      </span>
>      <xsl:value-of select="$suffix"/>
>    </xsl:template>
> 
> .... and the original function:
> 
>    <xsl:function name="bib:number-condense">
>      <xsl:param name="begin"/>
>      <xsl:param name="end"/>
>      <xsl:choose>
>        <xsl:when test="$begin castable as xs:integer">
>          <xsl:variable name="begin" select="$begin" as="xs:integer"/>
>          <xsl:choose>
>            <xsl:when test="$begin gt 100 and $begin mod 100 
> and $begin 
> idiv 100 eq $end idiv 100">
>              <xsl:value-of select="$end mod 100"/>
>            </xsl:when>
>            <xsl:otherwise>
>              <xsl:value-of select="$end"/>
>            </xsl:otherwise>
>          </xsl:choose>
>        </xsl:when>
>        <xsl:otherwise>
>          <xsl:value-of select="$end"/>
>        </xsl:otherwise>
>      </xsl:choose>
>    </xsl:function>
> 
> It has not failed yet (as in, Saxon never stops in its tracks with a 
> type error, and it formats the page ranges as I expect them to be 
> formatted), but it's not been widely tested.
> 
> Bruce

Current Thread