If conditions

An If element encloses any fragment of a template that should only apply under special circumstances. The condition is defined using the Test attribute, which holds an expression:

<if
					test="...">
  ...
  <!-- OPTIONAL FRAGMENT OF TEMPLATE -->
  ...
</if>

The expression must return a boolean result. A value of 'true' unlocks the content of the If element for use in the template. A value of 'false' indicates a failed test, and the content of the If element is ignored.

A template fragment can therefore be marked, then explicitly included or excluded using the 'true()' and 'false()' expressions:

<if test="true()">
  ...
  <!-- REQUIRED FRAGMENT OF TEMPLATE -->
  ...
</if>


<if test="false()">
  ...
  <!-- IGNORED FRAGMENT OF TEMPLATE -->
  ...
</if>

This simple technique could be used during stylesheet debugging, to enable and disable parts of templates used to provide output that assists with this task.

The expression appearing in the Test attribute takes the current element, and the current element list, as its context. This attribute is typically used to make decisions based on attribute values of the current element, or the presence (or absence) of specified sub-elements, as in the following examples.

This example shows how different prefix text may be generated, depending on the value of an attribute:

   <note>Normal note</note>
   <note type="secret">Secret note</note>


<xsl:template match="note">
  <P>
    <xsl:if test="@type='secret'">SECRET </xsl:if>
    <xsl:text>NOTE: </xsl:text>
    <xsl:apply-templates/>
  </P>
</xsl:template>


   <P>NOTE: Normal note</P>
   <P>SECRET NOTE: Secret note</note>

This example shows how a default title can be generated if there is no explicit title within a note:

<xsl:template match="note">
  <xsl:if test="not(title)"><P>NOTE</P></xsl:if>
  <xsl:apply-templates/>
</xsl:template>

A good example of the use of this element is illustrated in the standard. When formatting a list of names, the punctuation following each name depends on its location in the list. Commas are appropriate after each name, except for after the last one. In the following example, the test is only true when the name is not the last one in the list:

   <name>John</name>
   <name>Peter</name>
   <name>Lucy</name>


<if test="not(position() = last)"><text>, </text></if>

John, Peter, Lucy

The If element can be avoided by creating an additional template. The expression in the Test attribute is simply moved to both Match attributes, and in one case is enclosed by the 'not(…)' function. The following example avoids the If statement by testing for names at the ends of lists in a separate template. However, it can be argued that one of the main purposes of this command is precisely to avoid the need for additional templates:

<template match="name[not(position() = last)]">
  <apply-templates/><text>, </text>
</if>


<template match="name[position() = last]">
  <apply-templates/>
</if>

Other XSLT commands and output elements within the template must, as always, be well-formed. The If elements must therefore be placed with great care, both to fit into the well-formed structure and to avoid causing problems regardless of whether or not their content is enabled or disabled. Fortunately, the first requirement automatically takes care of the second. The following example is invalid, regardless of whether or not the test succeeds:

<xsl:template ...>
  <P>
    <xsl:if test="..."><B>...</xsl:test>
    <xsl:apply-templates/>
    </B>
  </P>
</xsl:if>

Finally, If elements can be nested within other If elements, to any number of levels.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset