XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Conditions with XSLT and XPath / Alternative in XPath: if - then - else
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)
XPath expressions can sometimes replace more extensive XSLT structures, as the previous chapter began to show when “tailoring” sequences. In that case, a series of xsl:value-of and xsl:text was replaced by a single instruction xsl:value-of with a suitable sequence expression.
A similar substitution is possible for xsl:if or a simple xsl:choose. XPath expressions can also contain conditions (Conditional Expressions). These are only simple IF-THEN-ELSE constructs, however; they cannot easily model a SELECT, meaning xsl:choose with several xsl:when. XPath IFs can, however, be nested:
if (bedingung) then (anweisung) else (alternative anweisung)
The ELSE branch is mandatory, but can be the empty sequence:
if (bedingung) then (anweisung) else ()
An XPath expression can therefore sometimes replace an xsl:if or an xsl:choose with one xsl:when and xsl:otherwise:
<xsl:value-of select="if (@isbn!='') then ('(ISBN: ', @isbn, ')') else ()"/>
Here the ISBN is tested and output within an expression in attribute select of xsl:value-of. The condition in parentheses after keyword if (test expression) tests, like the expression in attribute test of xsl:if or xsl:when, the presence and value of attribute isbn.
The sequence expression after keyword then (then expression) is output if the condition holds; otherwise the expression after keyword else (else expression) is evaluated. Here it is the empty sequence, so nothing is output. This corresponds to xsl:if. A nonempty else expression roughly reproduces xsl:choose.
Such expressions tend to become long or cryptic, so add explanatory comments. An XPath comment starts with (: and ends with :).
<xsl:value-of select="if (@isbn!='') then ('(ISBN: ', @isbn, ')') else() (: in diesem Fall nichts ausgeben :)"/>
Unlike most languages, XPath permits nested comments. The unusual “smiley comment” syntax results from ordinary comment markers such as /, * or # already having other uses in XPath. (: In jedem Falle wird der Quelltext freundlicher. :)
Line breaks between tokens are allowed; the expression need not occupy one line. Here is the equivalent of xsl:choose:
<xsl:value-of
select="if (@isbn!='')
then ('(ISBN: ', @isbn, ')')
else ('(ISBN-Nummer liegt nicht vor.)')"/>
In every case, the output consists of sequences taken by xsl:value-of and output as strings separated by spaces. Attribute separator can change this.
xsl:value-of: Pseudo-LREs with disable-output-escaping
Additional HTML markup is harder to output than in XSLT, because Literal Result Elements are not really available. (Note: XQuery has element constructors of the form element tagname {elementinhalt}, unfortunately unavailable in its XPath section. Otherwise, instead of <br/> one could write element br {}. Such an XPath extension is unlikely soon.) The following attempt to insert an HTML <br> before output causes an error:
<!-- Dies hier stellt einen Syntaxfehler dar: -->
<xsl:value-of select="if (@isbn!='') then ('<br/>(ISBN: ', @isbn, ')') else ('<br/>(ISBN-Nummer liegt nicht vor.)')"/>
In this context, tag angle brackets must be escaped as <br/>. The following variant is valid, but tags temporarily unescaped by the XML parser to <br/> are escaped again for output, which is not useful in the browser:
<xsl:value-of select="if (@isbn!='') then ('<br/>(ISBN: ', @isbn, ')') else ('<br/>(ISBN-Nummer liegt nicht vor.)')"/>
This escaping can be suppressed, however. xsl:value-of provides attribute disable-output-escaping. Value "yes" produces the desired HTML tag output:
<xsl:value-of select="if (@isbn!='') then ('<br/>(ISBN: ', @isbn, ')') else ('<br/>(ISBN-Nummer liegt nicht vor.)')" disable-output-escaping="yes"/>
Use this workaround cautiously: it disables output escaping for everything output by this instruction instance, not only the intended tags. Other special characters would remain unescaped, potentially making the result invalid as well-formed XML or XHTML.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn