Convert xs:double to xs:integer in xPath 2.0

Chan picture Chan · Jan 13, 2016 · Viewed 8.8k times · Source

I am trying to convert a xs:double to xs:integer in xPath 2.0 with functions (I don't want to use an XSLT).

number(//version//number/text())

The above output to - 6.0 (taking number as 6.0). How can I convert this to be 6?

Answer

raulk picture raulk · Jan 13, 2016

XPath 2.0 includes constructor functions for built-in XML Schema types, so you could do:

xs:integer(number(//version//number/text()))

See https://www.w3.org/TR/xpath-functions/#constructor-functions-for-xsd-types. I don't know what your environment is, but this worked without any special namespace configuration in my XML IDE. Your mileage may vary, in which case you'll need to find how to bind the XML Schema namespace to the xs: prefix in your environment.

By the way, since you are using // expressions which could return multiple elements, it may be safer to index //version and //number with [1], in case you only expect one occurrence: (//version[1]//number)[1].