xsl:if


The <xsl:if> instruction encloses a sequence constructor that will be evaluated only if a specified condition is true. If the condition is true, it returns the result of this evaluation; otherwise , it returns the empty sequence.

<xsl:if> is analogous to the if statement found in many programming languages.

Changes in 2.0

None.

Format

 <xsl:if   test = expression>   <!-- Content: sequence-constructor --> </xsl:if> 

Position

<xsl:if> is an instruction. It is always used within a sequence constructor.

Attributes

Name

Value

Meaning

test

Mandatory

Expression

The boolean condition to be tested

Content

A sequence constructor.

Effect

The test expression is evaluated. If the effective boolean value of the result is true, the contained sequence constructor is evaluated; otherwise, no action is taken (an empty sequence is returned).

Any XPath expression may be evaluated to obtain an effective boolean value. In brief, the rules are:

  • If the value of the expression is an empty sequence, the effective boolean value is false .

  • If the value of the expression is a single atomic value, the effective boolean value is false if the value is a zero-length string, the boolean value false , a number (integer, decimal, float, or double) equal to zero, or the special value NaN (not a number). Otherwise the effective boolean value is true .

  • If the value of the expression is a single node, the effective boolean value is true .

  • If the value of the expression is a sequence of two or more items, the effective boolean value is true .

These rules are the same as XPath itself uses in boolean contexts, for example in «if » expressions and for the operands of the «and » and «or » operators. They are also the same as the rules used by the boolean() function, and they are designed to be fully compatible with XPath 1.0. Note that the effective boolean value of a sequence can be evaluated without looking beyond the second item in the sequence, which makes the rules very efficient.

Usage

The <xsl:if> instruction is useful where an action is to be performed conditionally. It performs the functions of the if-then construct found in other programming languages. If there are two or more alternative actions (the equivalent of an if-then-else , switch , or Select Case in other languages), use <xsl:choose> instead.

One common use of <xsl:if> is to test for error conditions. In this case it is often used with <xsl:message> .

Try to avoid using <xsl:if> as the entire content of a template rule. It's better to use a predicate instead, because this gives the processor more scope for optimization. For example,

  <xsl:template match="para">   <xsl:if test="@display='yes'">   <p><xsl:apply-templates/></p>   </xsl:if>   </xsl:template>  

can be rewritten as:

  <xsl:template match="para[@display='yes']">   <p><xsl:apply-templates/></p>   </xsl:for-each>   <xsl:template match="para"/>  

(Of course, it's always possible that the processor will perform this optimization anyway, but you can't rely on it.)

Examples

The following example outputs an <hr> element after processing the last of a sequence of <para> elements:

  <xsl:template match="para">   <p><xsl:apply-templates/></p>   <xsl:if test="position()=last()">   <hr/>   </xsl:if>   </xsl:template>  

The following example reports an error if the percent attribute of the current element is not a number between 0 and 100. The expression returns true if:

  • the percent attribute does not exist

  • the value cannot be interpreted as a number (so that «number(@percent) » is NaN)

  • the numeric value is less than zero, or

  • the numeric value is greater than 100

  <xsl:if test="not(@percent) or   (string(number(@percent))='NaN') or   (number(@percent) lt 0) or   (number(@percent) gt 100)">   <xsl:message>   percent attribute must be a number between 0 and 100   </xsl:message>   </xsl:if>  

The next example shows the use of <xsl:if> in the context of a complete stylesheet.

Formatting a List of Names
start example

This example formats a list of names, using <xsl:if> to produce punctuation that depends on the position of each name in the list.

Source

The source file authors.xml contains a single <book> element with a list of authors.

  <?xml version="1.0"?>   <book>   <title>Design Patterns</title>   <author>Erich Gamma</author>   <author>Richard Helm</author>   <author>Ralph Johnson</author>   <author>John Vlissides</author>   </book>  

Stylesheet

The stylesheet authors.xsl processes the list of authors, adding punctuation depending on the position of each author in the list.

  <xsl:transform   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="1.0">   <xsl:template match="book">   <xsl:value-of select="title"/>   by <xsl:for-each select="author">   <xsl:value-of select="."/>   <xsl:if test="position()!=last()">, </xsl:if>   <xsl:if test="position()=last()-1">and </xsl:if>   </xsl:for-each>   </xsl:template>   </xsl:transform>  

Output

  Design Patterns   by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides  
end example
 

See Also

<xsl:choose> on page 236




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net