Booleans
A Boolean is a value that has exactly two states, true
or false. Every Boolean must have one of these binary values. XPath
does not provide any Boolean literals. If you use <xsl:value-of select="true"/>
in an
XSLT stylesheet, then the XSLT processor looks for a child element
of the context node named true
.
However, the XPath functions true()
and false( )
can
substitute for the missing literals quite easily.
Most of the time, however, Booleans are created by comparisons
between other objects, most commonly numbers. XPath provides all the
usual relational operators including =
, !=
,
<
, >
, >=
, and <=
. In addition, the and
and or
operators can combine Boolean
expressions according to the usual rules of logic.
Booleans are most commonly used in predicates of location
paths. For example, in the location step person[profession="physicist"]
, profession="physicist
" is a Boolean. It is
either true or false; there is no other possibility. Booleans are
also commonly used in the test
attribute of xsl:if
and xsl:when
elements. For example, this XSLT
template rule includes the profession
element in the output only if
its contents are “physicist” or “computer scientist”:
<xsl:template match="profession"> <xsl:if test=".='computer scientist' or .='physicist'"> <xsl:value-of select="."/> </xsl:if> </xsl:template>
This XSLT template rule italicizes the profession
element if and only if its
content is the string “computer scientist”:
<xsl:template match="profession"> <xsl:choose> <xsl:when ...
Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.