[2.0] Constructor Functions
XPath 2.0 introduces the idea of constructor
functions, which are similar to constructor functions in
object-oriented languages. As with object-oriented languages, the name
of the constructor function is the name of the datatype. Here’s how to
create a value of type xs:date:
<xsl:variable name="birthday" select="xs:date('1995-04-21')"/>This takes the string 1995-04-21 and creates a new value of type
xs:date. This can cause runtime
errors, as you would expect. A stylesheet that contains this
instruction won’t run at all:
<xsl:variable name="birthday" select="xs:date('next Tuesday')"/>This is a static error because the stylesheet processor knows
this instruction will fail. On the other hand, we’ll get a runtime
error if we send bad data to the xs:date constructor while the stylesheet is
being processed:
<xsl:variable name="birthday" select="xs:date(@birthday)"/>
This uses the birthday
attribute of the current node to create a new xs:date value. If that attribute contains a
value that can be cast as a xs:date, everything is fine; if the
attribute doesn’t contain valid data, we get a runtime error.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access