Multi‑branch

1. <xsl:choose> Multi‑branch Conditions

Syntax Structure (similar to if … else if … else)
<xsl:choose>
    <xsl:when test="condition‑expression">
        Output content when condition is true
    </xsl:when>
    <xsl:when test="condition‑expression2">
        Output content when condition2 is true
    </xsl:when>
    <xsl:otherwise>
        Output when none of the when conditions match (optional)
    </xsl:otherwise>
</xsl:choose>Code language: HTML, XML (xml)
  1. Matches top‑down; stops at the first satisfied <xsl:when>;
  2. <xsl:otherwise> must be placed after all <xsl:when> elements;
  3. In expressions, write > as &gt; and < as &lt;.
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head></head>
<body>
<ul>
    <!-- Iterate all students -->
    <xsl:for-each select="class/student">
        <!-- Sort by age ascending -->
        <xsl:sort select="age"/>
        <!-- Filter students taller than 160 -->
        <xsl:if test="height &gt; 160">
            <li>
                <xsl:value-of select="name"/>;
                <!-- Multi‑condition: bold age if height > 200 -->
                <xsl:choose>
                    <xsl:when test="height &gt; 200">
                        <b><xsl:value-of select="age"/></b>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="age"/>
                    </xsl:otherwise>
                </xsl:choose>
            </li>
        </xsl:if>
    </xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>Code language: HTML, XML (xml)

Execution flow: Iterate → sort by age → filter height>160 → bold age for entries with height over 200.

2. Browser‑Side XSLT Transformation

Not all browsers support XSLT. You can work around it using the approaches below. Nowadays most users use Chrome or Firefox, so this issue rarely occurs, but it is good to know.

1: JavaScript Client‑Side Transformation (Legacy IE approach)

Principle: Load JavaScript on page load, read XML file and XSL stylesheet, perform transformation inside browser and output HTML.

// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("cdcatalog.xml");
// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("cdcatalog.xsl");
// Transform and output
document.write(xml.transformNode(xsl));Code language: JavaScript (javascript)
  1. ActiveXObject is only available in old‑version IE; incompatible with modern Chrome, Firefox;
  2. Depends on browser‑built‑in XML parser with poor compatibility.
2: Server‑Side Transformation

Core idea: Do not perform transformation in browser

Most projects adopt this method: complete transformation on server side to eliminate browser compatibility concerns. Most enterprises implement it this way.

  1. Use XSLT engine on backend (Java / PHP / .NET etc.) to pre‑convert XML + XSL into XHTML;
  2. Send the generated HTML directly to browser;
  3. Advantage: Works on all browsers, client browser does not need XSLT support.

3. XPath Basic Concepts

XPath is an XML document path query language and the fundamental core of XSLT.

  1. Locate XML nodes with path expressions (similar to file paths);
  2. Built‑in standard function library supporting numeric calculation and string processing;
  3. Official W3C standard;
  4. All expressions inside select="" and test="" follow XPath syntax.

Summary

TagUsage ScenarioBranch Feature
<xsl:if>Single‑condition judgementOnly true branch, no else
<xsl:choose>Multi‑condition branchingMultiple when + otherwise (else)

Multi‑branch

Leave a Reply

Your email address will not be published. Required fields are marked *