XSLT - Working with Atomic Values and Type Conversion in XSLT 2.0

In XSLT 2.0, an atomic value is a single value that is not a node. Atomic values are an important part of XSLT because XSLT 2.0 is based on the XPath 2.0 data model, which provides strong support for different data types. Examples of atomic values include strings, numbers, Boolean values, dates, times, and durations. When processing XML, XSLT can extract information from nodes and convert that information into appropriate atomic types for calculations, comparisons, and other operations.

What Are Atomic Values?

An atomic value represents one indivisible piece of data. Unlike XML nodes such as elements and attributes, an atomic value does not have child nodes or attributes.

For example, consider this XML:

<student>
    <name>Rahul</name>
    <age>20</age>
    <percentage>85.5</percentage>
</student>

The <name>, <age>, and <percentage> elements are nodes. However, when their values are extracted, they can be treated as atomic values.

For example:

<xsl:value-of select="student/name"/>

produces the string value:

Rahul

Similarly:

<xsl:value-of select="student/age"/>

produces:

20

The extracted value can then be converted to a more specific type when required.

Common Atomic Data Types

XSLT 2.0 supports several important atomic data types inherited from XML Schema.

String

The xs:string type represents textual information.

<xsl:variable name="name" as="xs:string" select="'Rahul'"/>

Here, the variable contains a string value.

Integer

The xs:integer type represents whole numbers.

<xsl:variable name="age" as="xs:integer" select="20"/>

This is useful when performing mathematical operations or numerical comparisons.

Decimal

The xs:decimal type represents decimal numbers.

<xsl:variable name="marks" as="xs:decimal" select="85.5"/>

This allows the value to participate in decimal calculations.

Double

The xs:double type represents floating-point numbers.

<xsl:variable name="value" as="xs:double" select="12.75"/>

Boolean

The xs:boolean type represents either true or false.

<xsl:variable name="passed" as="xs:boolean" select="true()"/>

Date

XSLT 2.0 can work with dates using the xs:date type.

<xsl:variable name="joiningDate" as="xs:date"
              select="xs:date('2026-10-06')"/>

This is particularly useful when XML documents contain dates that need to be compared or manipulated.

Why Type Conversion Is Needed

XML content is commonly represented as text. Even when a value looks like a number, the processor may initially treat it as text depending on how it is obtained and the context in which it is used.

Suppose an XML document contains:

<price>500</price>

If the price is treated as a string, numerical operations may not behave as intended. Converting it to an integer or decimal makes its intended purpose explicit.

For example:

<xsl:variable name="price" as="xs:decimal"
              select="xs:decimal(price)"/>

Now the value can be used in calculations:

<xsl:value-of select="$price * 2"/>

The result is:

1000

Explicit Type Conversion

XSLT 2.0 provides constructors such as xs:string(), xs:integer(), xs:decimal(), xs:date(), and xs:boolean() for converting values.

For example:

<xsl:variable name="number" select="xs:integer('100')"/>

The string "100" is converted into an integer.

Another example:

<xsl:variable name="amount" select="xs:decimal('125.50')"/>

The text "125.50" is converted into a decimal value.

A date can similarly be converted:

<xsl:variable name="date"
              select="xs:date('2026-10-06')"/>

The value is now treated as an actual date rather than ordinary text.

Using Atomic Values in Calculations

One of the major advantages of atomic values is that they can be used directly in expressions.

Consider:

<xsl:variable name="price" as="xs:decimal" select="250.00"/>
<xsl:variable name="quantity" as="xs:integer" select="4"/>

<xsl:value-of select="$price * $quantity"/>

The output is:

1000

Because both variables represent numerical atomic values, XSLT can perform the multiplication directly.

A discount can also be calculated:

<xsl:variable name="price" as="xs:decimal" select="1000"/>
<xsl:variable name="discount" as="xs:decimal" select="10"/>

<xsl:value-of select="$price - ($price * $discount div 100)"/>

The result is:

900

Type Conversion with XML Data

Consider the following XML:

<products>
    <product>
        <name>Laptop</name>
        <price>55000</price>
    </product>
    <product>
        <name>Tablet</name>
        <price>25000</price>
    </product>
</products>

The price values can be converted into decimals while processing the document:

<xsl:for-each select="products/product">
    <xsl:value-of select="name"/>
    <xsl:text> - </xsl:text>
    <xsl:value-of select="xs:decimal(price)"/>
    <xsl:text>&#10;</xsl:text>
</xsl:for-each>

This makes the numerical nature of the price explicit and allows it to be used in further calculations.

Type Conversion and Comparisons

Atomic values are also useful when comparing data.

For example:

<xsl:if test="xs:integer(age) >= 18">
    <xsl:text>Adult</xsl:text>
</xsl:if>

Here, the age value is converted to an integer before comparison.

Without appropriate typing, a comparison involving textual representations of numbers can produce results that do not match normal numerical expectations.

For example, comparing textual values is conceptually different from comparing numerical values. Converting the data to the intended type avoids such ambiguity.

Typed Variables

XSLT 2.0 allows a variable's expected type to be specified using the as attribute.

<xsl:variable name="age" as="xs:integer" select="25"/>

This tells the XSLT processor that the variable is expected to contain an integer.

Similarly:

<xsl:variable name="name" as="xs:string"
              select="'Anita'"/>

and:

<xsl:variable name="active" as="xs:boolean"
              select="true()"/>

Typed variables make stylesheets easier to understand and can help identify type-related errors.

Declaring the XML Schema Namespace

When using types such as xs:string, xs:integer, and xs:decimal, the XML Schema namespace is normally declared in the stylesheet:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

A basic stylesheet could therefore look like:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:template match="/">
        <xsl:variable name="age" as="xs:integer"
                      select="xs:integer('20')"/>

        <xsl:value-of select="$age + 5"/>
    </xsl:template>

</xsl:stylesheet>

The result is:

25

Benefits of Atomic Values and Type Conversion

Working with atomic values provides several benefits in XSLT 2.0. It makes numerical calculations more reliable, allows dates and times to be handled properly, makes comparisons more predictable, and allows stylesheet variables and expressions to have clearly defined types. Type conversion is especially important when XML data comes from sources where values are represented primarily as text.

Atomic values and type conversion therefore provide the foundation for stronger data handling in XSLT 2.0. They allow an XSLT stylesheet to distinguish between text, numbers, Boolean values, dates, and other data types instead of treating every piece of information simply as text. This becomes particularly valuable when building transformations that perform calculations, validate values, compare dates, process numerical data, or apply different operations according to the data type.