Advanced XSLT: Conditions, Sorting, and Modular Design
Unit 4âĸCLO04, CLO05
Learning Objectives
Course Learning Outcomes
CLO04
CLO05
Course Outcomes
CO04
CO05
âšī¸
Introduction
Advanced transformations require branching logic, computed values, and structured templates. This topic introduces conditional constructs, sorting, and maintainable stylesheet organization.
The Basics
Conditional logic
xsl:if: single conditionxsl:choose: if/else-if/else
Technical Details
choose example
<xsl:choose>
<xsl:when test="price > 500">Expensive</xsl:when>
<xsl:otherwise>Affordable</xsl:otherwise>
</xsl:choose>
Sorting
<xsl:for-each select="library/book">
<xsl:sort select="number(price)" data-type="number" order="descending"/>
<xsl:value-of select="title"/>
</xsl:for-each>
Examples
Example: classify a node
<xsl:template match="book">
<tr>
<td><xsl:value-of select="title"/></td>
<td>
<xsl:choose>
<xsl:when test="number(price) > 500">High</xsl:when>
<xsl:otherwise>Low</xsl:otherwise>
</xsl:choose>
</td>
</tr>
</xsl:template>
Real-World Use
Practical
- Add conditional output (labels or styling).
- Add computed values (totals/averages).
đ For exams
Exam
- Differentiate xsl:if and xsl:choose.
- Explain xsl:sort usage.
⨠Key points
Takeaways
- Use choose for robust branching.
- Sort with xsl:sort; convert to numbers when needed.