Selections

Selections in xslt come in two ways, what else is new:

Example 42.5. Selection if: browsere3.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/1999/xhtml">

  <xsl:output doctype-system="about:legacy-compat"
        indent="yes"
        method="xml"
        omit-xml-declaration="no"/>

  <xsl:template match="products">
    <html lang="en">
    <head>
      <title>Test if</title>
      <meta charset="utf-8"/>
    </head>
    <body>
    <h1>Products, alphabetically</h1>
    <ol>
    <xsl:apply-templates select="item">
      <xsl:sort/>
    </xsl:apply-templates>
    </ol>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <xsl:if test="@type='P'">
      <li>
        Primary browsers: <xsl:apply-templates/>
      </li>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Try it!


Example 42.6. Selection choose: browsere4.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/1999/xhtml">

  <xsl:output doctype-system="about:legacy-compat"
        indent="yes"
        method="xml"
        omit-xml-declaration="no"/>

  <xsl:template match="products">
    <html xml:lang="en" lang="en">
    <head>
      <title>Test</title>
      <meta charset="utf-8"/>
    </head>
    <body>
    <h1>Products, alphabetically</h1>
    <ol>
    <xsl:apply-templates select="item">
      <xsl:sort select="@type"/>
    </xsl:apply-templates>
    </ol>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <xsl:choose>
      <xsl:when test="@type='P'"><li>Primary: <xsl:apply-templates/></li></xsl:when>
      <xsl:when test="@type='O'"><li>Others: <xsl:apply-templates/></li></xsl:when>
      <xsl:otherwise><li>WTF? <xsl:apply-templates/></li></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

Try it!