Question

I am using the number() function in my XSLT markup in order to translates values, in this case odds from a casino, into JSON. Example XML looks like the following:

<Casino ClientID="116" Name="5Dimes">-116</Casino>
<Casino ClientID="104" Name="BOVADA">+104</Casino>

My markup looks like the following:

"line": <xsl:call-template name="odds_check_tpl"><xsl:with-param name="odds_field" select="text()" /></xsl:call-template>

And the odds_check_tpl is written like this:

   <xsl:template name="odds_check_tpl">
      <xsl:param name="odds_field" />
        <xsl:choose>
           <xsl:when test="string($odds_field) != 'NaN'"><xsl:value-of select="number($odds_field)" /></xsl:when>
           <xsl:otherwise>"Not Available Yet"</xsl:otherwise>
        </xsl:choose>
    </xsl:template>

The problem I'm having is that when I attempt to parse -116 this comes out perfectly. For +104 however, I get a NaN. I'm guessing the number() function treats - as negatives but gets tricked out when an explicit + is put in front of a number. Is there a way to work around this so I can treat these values as numbers in my JSON vs having to use a string?

Was it helpful?

Solution

Why don't you simply translate the + sign out, for example:

<xsl:call-template name="odds_check_tpl">
    <xsl:with-param name="odds_field" select="translate(., '+', '')" />
</xsl:call-template>

Then, assuming odds cannot be zero, you can simplify your test to:

<xsl:choose>
    <xsl:when test="number($odds_field)">
        <xsl:value-of select="$odds_field" />
    </xsl:when>
<xsl:otherwise>"Not Available Yet"</xsl:otherwise>
</xsl:choose>   

Edit

If you want to preserve the + sign at the output, then call the template without translating:

<xsl:call-template name="odds_check_tpl">
    <xsl:with-param name="odds_field" select="." />
</xsl:call-template>

and do the test as:

<xsl:choose>
    <xsl:when test="number(translate($odds_field, '+', ''))">
        <xsl:value-of select="$odds_field" />
    </xsl:when>
<xsl:otherwise>"Not Available Yet"</xsl:otherwise>
</xsl:choose>

--
Let me just stress again that this assumes odds are not supposed to be zero (as this is not at all obvious from the examples given); in such case the result will be ""Not Available Yet".

OTHER TIPS

You can strip the + before converting it. This will create a variable $number containing either a number or NaN:

<xsl:variable name="number">
    <xsl:choose>
        <xsl:when test="substring($odds_field, 1, 1) = '+'">
            <xsl:value-of select="number(substring($odds_field, 2))" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="number($odds_field)" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

You can then use it in your test:

<xsl:choose>
    <xsl:when test="$number != 'NaN'">
        <xsl:value-of select="$number" />
    </xsl:when>
    <xsl:otherwise>"Not Available Yet"</xsl:otherwise>
</xsl:choose>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top