# Test for a negative number

### Question

• Hi all,

I have scoured the Internet for the answer but to no avail.

How can I test whether a number is negative? (I need 0 to be classed as a positive number).

Is the following good enought...?

<xsl:when test="number(\$CreditLimit) = number(\$CreditLimit)">
...DO SOMETHING...
</xsl:when>

Thanks
Thursday, August 21, 2008 9:57 AM

• I am not sure I understand the problem. A number is negative if it is less than zero so

Code Snippet

<xsl:if test="\$CreditLimit &lt; 0">

should do.

Thursday, August 21, 2008 11:12 AM
• Use the standard "<" (must be written escaped as "&lt;") XPath operator.

In XPath 2.0 one can use the "lt" operator for comaring atomic values -- this is usually faster than using the "<" operator.

Similarly in XPath 2.0 and 2.0:

"&lt;=", "=", ">", ">="

In XPath 2.0:

"lt", "gt", "eq", "le", "ge"

Cheers,

Dimitre Novatchev

Thursday, August 21, 2008 3:09 PM
•  Tryst wrote:
 Hi and thanks for the replies all.Basically, I have the following template which set a variable to 0 is a passed in number is negative, else use the positive number, but a negative number that gets passed in always gets passed back out as negative...                                                                                                                                0                                                        Is there something simple that I am missing here because it all seems right to me.Thanks

In the code provided, there is nothing that does what you describe as: "else use the positive number"

The code just produces the number, if it is negative, or 0 otherwise.

It is not clear from the description what the code is supposed to do.

Cheers,

Dimitre Novatchev

Friday, August 22, 2008 12:20 AM
•  Pawel Kadluczka - MSFT wrote:
 Actually your code does not do what you describe. Try this:   Code Snippet 0       Hope this helps Pawel

Which is equivalent to the following single and shorter XPath expression:

number(\$CreditLimit > 0) * \$CreditLimit

Thanks,

Dimitre Novatchev

Friday, August 22, 2008 1:29 AM
•  number(\$CreditLimit > 0) * \$CreditLimit

Tryst wrote:
 Dimitre, is your shorthand version only allowed in XPath 2.0 though?Cheers

No, this is standard XPath 1.0.  What XPath 2.0 features do you see in the expression?

The simplest verification whether or not this expression is a valid XPath 1.0 one, just use it in an xslt stylesheet with an XSLT 1.0 processor

In this and quite a few other cases one may avoid using an <xsl:choose> instruction.

Here we are using the fact that by definition

number(true()) = 1

and

number(false()) = 0

Thanks,

Dimitre Novatchev

Sunday, August 24, 2008 4:21 AM
•  AlexBB wrote:
 Pawel, I tried to study the thread and ran into a few problems. I focused on your code sample. I tried to open it in an XmlNotepad. exe 2007 and could not. The error messahe was that the namespace on line 1 could not be found. Also I noticed that a node is not closed in you code. I wonder if it is an omission/oversight or the closing element is not needed for some reason.   I am posting you code sample with the statement I inserted which will be highlighted.   Could you dispell my confusion with some explanation please? Thanks. Code Snippet   0       Also I am wondering if perhaps should also be there as well?   Has the namespace statement been left outside of the code samples to focus on this template fragment only?

Two things:

1. The <xsl:template> xslt instruction must be a child of a <xsltylesheet> element, which is the top (and single) element of the xslt stylesheet. The "xsl namespace" is typically declared on this top element, like in the following example:

<xsltylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

2. There is no non-matching </xslaram> in the code posted by Pawel.

Thanks,

Dimitre Novatchev

Sunday, August 24, 2008 8:44 PM

### All replies

• I am not sure I understand the problem. A number is negative if it is less than zero so

Code Snippet

<xsl:if test="\$CreditLimit &lt; 0">

should do.

Thursday, August 21, 2008 11:12 AM
• FYI - the test from your post (i.e. number(\$CreditLimit) = number(\$CreditLimit)) can be used to verify if the argument is a number.

Pawel

Thursday, August 21, 2008 2:59 PM
• Use the standard "<" (must be written escaped as "&lt;") XPath operator.

In XPath 2.0 one can use the "lt" operator for comaring atomic values -- this is usually faster than using the "<" operator.

Similarly in XPath 2.0 and 2.0:

"&lt;=", "=", ">", ">="

In XPath 2.0:

"lt", "gt", "eq", "le", "ge"

Cheers,

Dimitre Novatchev

Thursday, August 21, 2008 3:09 PM
• Hi and thanks for the replies all.

Basically, I have the following template which set a variable to 0 is a passed in number is negative, else use the positive number, but a negative number that gets passed in always gets passed back out as negative...

<!-- Set CreditLimit to '0' for negative values -->
<xsl:template name="SetCreditLimit">
<xslaram name="CreditLimit" />

<xsl:variable name="SetCreditLimit">
<xsl:choose>
<xsl:when test="\$CreditLimit &Lt; 0">
<xsl:value-of select="\$CreditLimit" />
</xsl:when>
<xsltherwise>
0
</xsltherwise>
</xsl:choose>
</xsl:variable>

<xsl:value-of select="\$SetCreditLimit" />
</xsl:template>

Is there something simple that I am missing here because it all seems right to me.

Thanks
Thursday, August 21, 2008 10:24 PM
•  Tryst wrote:
 Hi and thanks for the replies all.Basically, I have the following template which set a variable to 0 is a passed in number is negative, else use the positive number, but a negative number that gets passed in always gets passed back out as negative...                                                                                                                                0                                                        Is there something simple that I am missing here because it all seems right to me.Thanks

In the code provided, there is nothing that does what you describe as: "else use the positive number"

The code just produces the number, if it is negative, or 0 otherwise.

It is not clear from the description what the code is supposed to do.

Cheers,

Dimitre Novatchev

Friday, August 22, 2008 12:20 AM
• Actually your code does not do what you describe. Try this:

Code Snippet

<xsl:template name="SetCreditLimit">

<xsl:param name="CreditLimit" />

<xsl:choose>

<xsl:when test="\$CreditLimit &lt; 0">0</xsl:when>

<xsl:otherwise>

<xsl:value-of select="\$CreditLimit" />

<xsl:otherwise>

</xsl:choose>

</xsl:template>

Hope this helps

Pawel

Friday, August 22, 2008 12:32 AM
•  Pawel Kadluczka - MSFT wrote:
 Actually your code does not do what you describe. Try this:   Code Snippet 0       Hope this helps Pawel

Which is equivalent to the following single and shorter XPath expression:

number(\$CreditLimit > 0) * \$CreditLimit

Thanks,

Dimitre Novatchev

Friday, August 22, 2008 1:29 AM
• Dimitre, is your shorthand version only allowed in XPath 2.0 though?

Cheers
Saturday, August 23, 2008 8:41 PM
•  number(\$CreditLimit > 0) * \$CreditLimit

Tryst wrote:
 Dimitre, is your shorthand version only allowed in XPath 2.0 though?Cheers

No, this is standard XPath 1.0.  What XPath 2.0 features do you see in the expression?

The simplest verification whether or not this expression is a valid XPath 1.0 one, just use it in an xslt stylesheet with an XSLT 1.0 processor

In this and quite a few other cases one may avoid using an <xsl:choose> instruction.

Here we are using the fact that by definition

number(true()) = 1

and

number(false()) = 0

Thanks,

Dimitre Novatchev

Sunday, August 24, 2008 4:21 AM
• Pawel, I tried to study the thread and ran into a few problems. I focused on your code sample. I tried to open it in an XmlNotepad. exe 2007 and could not. The error messahe was that the namespace on line 1 could not be found. Also I noticed that a node is not closed in you code. I wonder if it is an omission/oversight or the closing element is not needed for some reason.

I am posting you code sample with the statement I inserted which will be highlighted.

Could you dispell my confusion with some explanation please? Thanks.

Code Snippet

<xsl:template name="SetCreditLimit">

<xsl:param name="CreditLimit" />

<xsl:choose>

<xsl:when test="\$CreditLimit &lt; 0">0</xsl:when>

<xsl:otherwise>

<xsl:value-of select="\$CreditLimit" />

<xsl:otherwise>

</xsl:choose>

</xsl:param>

</xsl:template>

Also I am wondering if perhaps </xsl:when> should also be there as well?

Has the namespace statement been left outside of the code samples to focus on this template fragment only?

Sunday, August 24, 2008 6:12 PM
•  AlexBB wrote:
 Pawel, I tried to study the thread and ran into a few problems. I focused on your code sample. I tried to open it in an XmlNotepad. exe 2007 and could not. The error messahe was that the namespace on line 1 could not be found. Also I noticed that a node is not closed in you code. I wonder if it is an omission/oversight or the closing element is not needed for some reason.   I am posting you code sample with the statement I inserted which will be highlighted.   Could you dispell my confusion with some explanation please? Thanks. Code Snippet   0       Also I am wondering if perhaps should also be there as well?   Has the namespace statement been left outside of the code samples to focus on this template fragment only?

Two things:

1. The <xsl:template> xslt instruction must be a child of a <xsltylesheet> element, which is the top (and single) element of the xslt stylesheet. The "xsl namespace" is typically declared on this top element, like in the following example:

<xsltylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

2. There is no non-matching </xslaram> in the code posted by Pawel.

Thanks,

Dimitre Novatchev

Sunday, August 24, 2008 8:44 PM
• There is no non-matching </< FONT>xslaram> in the code posted by Pawel

Dimitre, I am sorry, but after I posted that I tried to work with the xml code again and the same problems kept coming. After I did some surgery which appears tto be natural to me, the file was revealed in the Notepad in all its glory

Code Snippet

<xsltylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="SetCreditLimit">

<xsl:param name="CreditLimit" />

<xsl:choose>

<xsl:when test="\$CreditLimit &lt; 0">0</xsl:when>

<xsl:otherwise>

<xsl:value-of select="\$CreditLimit" />

</xsl:otherwise>

</xsl:choose>

</xsl:template>

</xsltylesheet>

Thanks
Monday, August 25, 2008 12:45 PM