Skip to main content
XMLDir

Search 169 pages — formats, namespaces, parser messages, schema errors and tools.

XPath function

number()

Converts to a number, producing NaN rather than an error when it cannot — which is how bad data passes silently.

number() converts its argument to a number. A string that is not a valid number becomes NaN, and NaN compares false against everything, including itself.

That makes NaN hard to detect. The idiom is `number(x) != number(x)`, which is only true for NaN.

Syntax
number(object?) → number
Returns
number
Kind
Function
Version
XPath 1.0 (libxml2)

Worked examples

Each result below is what the evaluator returned, asserted on every test run. An empty result means an empty node-set — which is an answer, not an error.

  • number('14.50')on order.xmlRun
    14.5
  • number('14,50')on order.xmlRun
    NaN

    A comma decimal separator is not a number to XPath.

  • number('x') = number('x')on order.xmlRun
    false

    NaN is not equal to itself. This is how you test for it.

Run your own expression

What catches people out

  • NaN silently poisons arithmetic and every comparison it takes part in.
  • Leading and trailing whitespace is allowed; anything else is not. '1 200' and '1,200' are both NaN.
  • Booleans convert to 1 and 0, so number(true()) is 1.

The sample documents

Every example on this page runs against this document. The sitemap one is namespaced on purpose — almost every real XML document is, and that changes which expressions match.

order.xml
<?xml version="1.0" encoding="UTF-8"?>
<order id="A-1001" tier="gold">
  <customer country="NO">Ada Lovelace</customer>
  <lines>
    <line sku="WIDGET-1" qty="2"><price>14.50</price></line>
    <line sku="WIDGET-2" qty="1"><price>39.00</price></line>
    <line sku="GIFT-WRAP" qty="1"><price>0.00</price></line>
  </lines>
  <note>  Deliver after 09:00  </note>
</order>

Related

All 34 XPath entries

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.