January 7, 2002 - Printing Conditional Templates in XSLT | WebReference

January 7, 2002 - Printing Conditional Templates in XSLT

Yehuda Shiran January 7, 2002
Printing Conditional Templates in XSLT
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Conditional templates are output only if certain conditions exist within the source document. Conditional templates are defined with either <xsl:if> or <xsl:choose> elements. In the following report card, the grade element has an attribute named graduate. You might want to generate some output (a * next to the grade, for example) only when this attribute has a certain value:

<?xml version="1.0"?>
<report-card>
  <grade graduate="yes">
    <number>cs101</number>
    <name>Introduction to Computer Science</name>
    <instructor>Prof. Johnson</instructor>
    <score>82</score>
  </grade>
  <grade graduate="no">
    <number>ee105</number>
    <name>Networks</name>
    <instructor>Prof. Smith</instructor>
    <score>73</score>
  </grade>
  <grade graduate="yes">
    <name>ce105</name>
    <instructor>Prof. Vered</instructor>
    <score>100</score>
  </grade>
</report-card>
Here is XSL code that will test the graduate field and will print a * if the answer is positive:
<xsl:if test="@graduate[.='yes']">*</xsl:if>