Conditions

It is possible to vary the output of a template depending on certain conditions. In the simplest case, part of the formatting can be optional, and only instantiated when a specific condition is true.

Simple test

The If element encloses optional formatting instructions, and uses a Test attribute to determine whether or not the content is to be made an active part of the transformation. The test is an XPath expression. When the Test attribute contains only an element name or attribute name, the test returns 'true' when the element is present as a child of the current element, or the attribute is attached to the current element:

<if test="secret">
  <X:title>THIS ELEMENT CONTAINS A SECRET ELEMENT</X:title>
</if>

This feature can be used to reduce the number of templates needed. For example, if alternating paragraphs are to be coloured differently, the first template below produces the same effect as the following two. Note that, to keep the example well-formed, it is necessary to specify the HTML open tag once, then use the Attribute element to add an appropriate attribute value to this element (the attribute is not added to the If element, but to the nearest output element, which in this case is the P element):

<template match="para">
  <html:P>
    <if test="position() mod 2 = 0">
      <attribute name="STYLE">color: red</attribute>
    </if>
    <if test="not(position() mod 2 = 0)">
      <attribute name="STYLE">color: blue</attribute>
    </if>
    <apply-templates/>
  </html:P>
</template>


<!-- VERBOSE ALTERNATIVE -->

<template match="para[position() mod 2 = 0]">
  <html:P STYLE="color: red">
    <apply-templates/>
  </html:P>
</template>

<template match="para[not(position() mod 2 = 0)]">
  <html:P STYLE="color: blue">
    <apply-templates/>
  </html:P>
</template>

The saving can be more significant. In the following example an attribute can take a number of possible values, each one producing a different format:

   <para type="normal">A normal paragraph</para>
   <para type="secret">A secret paragraph</para>
   <para type="optional">An optional paragraph</para>


<template match="para">
  <html:P>
  <if test="@type='normal'">
    <attribute name="STYLE">color: black</attribute>
  </if>
  <if test="@type='secret'">
    <attribute name="STYLE">color: red</attribute>
  </if>
  <if test="@type='optional'">
    <attribute name="STYLE">color: green</attribute>
  </if>
  <apply-templates/></html:P>
</template>



   <P STYLE="color: black">A normal paragraph</P>
   <P STYLE="color: red">A secret paragraph</P>
   <P STYLE="color: green">An optional paragraph</P>

However, this technique can still be quite clumsy. It does not prevent two or more tests from succeeding, and does not provide for a catch-all condition to be activated in the event that all the tests fail.

Choice and default

The Choose element is more flexible than the If element. It contains When elements for each case, and an Otherwise element that is activated when none of the explicit cases apply. The When element has a Test attribute, which works as described for the If element:

<template match="para">
  <html:P>
  <choose>
    <when test="@type='normal'">
      <attribute name="STYLE">color:black</attribute>
    </when>
    <when test="@type='secret'">
      <attribute name="STYLE">color:red</attribute>
    </when>
    <when test="@type='optional'">
      <attribute name="STYLE">color:green</attribute>
    </when>
    <otherwise>
      <attribute name="STYLE">color:yellow</attribute>
    </otherwise>
    ...
  </choose>
  <apply-templates/>
  </html:P>
</template>

Only the first successful test is chosen; the remaining tests are ignored. Only if none of the tests succeed does the content of the Otherwise element become active.

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

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