O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

[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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required