[2.0] Datatype Operators—instance of, castable as, cast as, and treat as

As you would expect, a language that supports datatypes and constructors also has operators to convert a value from one type to another. XPath 2.0 provides four operators: instance of, castable as, cast as, and treat as. We’ll cover those here.

instance of

The instance of operator lets us see whether a value is an instance of a particular datatype. Here are some examples:

<?xml version="1.0"?>
<!-- instance-of.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:text>&#xA;Some tests of the "instance of" operator:</xsl:text> <xsl:text>&#xA;&#xA; '1995-04-21' instance of xs:date: </xsl:text> <xsl:value-of select="'1995-04-21' instance of xs:date"/> <xsl:text>&#xA; xs:date('1995-04-21') instance of xs:date: </xsl:text> <xsl:value-of select="xs:date('1995-04-21') instance of xs:date"/> <xsl:text>&#xA;&#xA; 3 instance of xs:integer: </xsl:text> <xsl:value-of select="3 instance of xs:integer"/> <xsl:text>&#xA; '3' instance of xs:integer: </xsl:text> <xsl:value-of select="'3' instance of xs:integer"/> <xsl:text>&#xA; number('3') instance of xs:integer: </xsl:text> <xsl:value-of select="number('3') instance of xs:integer"/> <xsl:text>&#xA; number('3') instance of xs:double: </xsl:text> <xsl:value-of select="number('3') instance of xs:double"/> <xsl:text>&#xA; xs:integer('3') instance ...

Get XSLT, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.