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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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>

People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business