XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (107 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.42Mb size Format: txt, pdf, ePub

XPath 2.0 also allows casting from an
xs:dateTime
or
xs:date
to any of these five types: the relevant components (including the timezone) are extracted, and the other components are discarded. The full rules for casting between different types are given in Chapter 11.

Binary Types

XML Schema supports two types for holding binary data (for example, images or sound clips). These are
xs:base64Binary
and
xs:hexBinary
. Binary data cannot be held in an XML document directly, so it is always encoded as characters, and these two types support the two most popular encodings.

Base 64 encoding is defined by reference to Internet mail standards in RFC 2045 (
http://www.ietf.org/rfc/rfc2045
), though the format was originally described in RFC 1421. The basic idea is that the binary stream is split into 24-bit chunks (3 bytes), and each chunk is then considered as four groups of 6 bits. Each 6-bit group is then considered to be the code representing a character in an alphabet of 64 characters, and this character is used to represent the value in the lexical representation. The 64-character alphabet consists of A–Z, a–z, 0–9,
+
, and
/
. One or more
=
characters may occur at the end to indicate padding to a whole number of 8-bit bytes, and newlines may appear to break up the total sequence (according to the RFC, the maximum line length is 76 characters).

The
hexBinary
encoding is simpler but less compact: it simply takes each octet of the binary stream, and represents it as two hexadecimal digits.

XPath 2.0 doesn't offer any very useful functionality for these two types. In particular, it doesn't provide you with any way to convert the values to or from an actual stream of octets. What you can do is to compare the values for equality, convert them to and from strings, and convert between the two types, in either direction. If you need more than that, Saxon offers some extension functions.

Single-Precision Floating Point

Unlike the other types that I've classified as minor,
xs:float
is well supported by functions and operators in XPath 2.0; in fact, any operator or function that can be applied to an
xs:double
can also be applied to an
xs:float
.

There is no numeric literal for
xs:float
values. You have to create them using a constructor function, for example
xs:float(3.14159)
.

The real reason I have classified
xs:float
as a minor type is that I can't see any reason why anyone should want to use it. Compressing a floating-point number into 32 bits made sense in the 1960s, but it makes little sense nowadays, and the loss of precision when performing numeric calculations is far too severe for most applications to justify the space saving. The only justification I have heard for including this type in XML Schema is for compatibility with other (older) type systems such as SQL.

In XML Schema,
xs:float
is not defined as a subtype of
xs:double
. Its value space is a strict subset of
xs:double
, but the working group decided to make it a primitive type apparently because of the difficulty of defining the nature of the restriction, which would have required the invention of new facets. In XPath, however,
xs:float
can be considered for most practical purposes to be a subtype of
xs:double
. It won't pass explicit tests such as
$F instance of xs:double
that it would pass if it were a true subtype, but you can pass an
xs:float
value to any function or operator that expects an
xs:double
, and it will be converted automatically (this particular kind of conversion is referred to as numeric promotion).

When you mix
xs:float
and
xs:double
in a calculation, the result is
xs:double
. If you mix
xs:float
and
xs:decimal
, however, the result is
xs:float
.

The xs:duration Type

The
xs:duration
type is one of the primitive types in XML Schema, but as we've already seen, XPath decided to avoid the difficulties it posed by introducing the two subtypes
xs:yearMonthDuration
and
xs:dayTimeDuration
. You can still use the
xs:duration
type in your schema and in your documents, but there is relatively little support for it in XPath. You can compare one
xs:duration
to test whether it is equal to another, but you can't apply any ordering tests, and you can't perform any arithmetic operations. You can extract the components of the duration—it will be normalized so that P18 M is treated as one year six months, and PT36H is treated as one day 12 hours, but months will never be converted to days or vice versa. A few conversions are also allowed: you can convert a string to an
xs:duration
, and convert an
xs:duration
to a string. You can also convert an
xs:duration
to an
xs:yearMonthDuration
or
xs:dayTimeDuration
, which is done by removing the components that aren't applicable to the target type.

Other books

A New Beginning by Johnson, J
Angel In My Bed by Melody Thomas
Codependently Yours by Maria Becchio
West of Here by Jonathan Evison
Chessmen of Doom by John Bellairs
First Night: by Anna Antonia
Soulmates by Holly Bourne