XSLT Programmatically

Look at the following xml document, and let us discuss some corresponding xslt possibilities:

Example 42.1. browsere.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="browsere.xsl"?>
<products>
  <item kat="browser" type="O">HotJava</item>
  <item kat="browser" type="P">Internet Explorer</item>
  <item kat="browser" type="O">Midori</item>
  <item kat="browser" type="O">Minimo</item>
  <item kat="browser" type="P">Epiphany</item>
  <item kat="browser" type="O">Flock</item>
  <item kat="browser" type="P">Opera</item>
  <item kat="browser" type="P">Safari</item>
  <item kat="browser" type="P">Seamonkey</item>
  <item kat="browser" type="P">Konqueror</item>
  <item kat="browser" type="O">Lobo</item>
  <item kat="browser" type="O">3B</item>
  <item kat="browser" type="O">Arora</item>
  <item kat="browser" type="O">Shiira</item>
  <item kat="browser" type="P">Chrome</item>
  <item kat="browser" type="O">DeepFish</item>
  <item kat="browser" type="O">Doris</item>
  <item kat="browser" type="P">Firefox</item>
  <item kat="browser" type="O">Sleipnir</item>
  <item kat="browser" type="O">Slim Browser</item>
  <item kat="browser" type="O">Space Time 3D</item>
  <item kat="browser" type="O">Browse 3D</item>
  <item kat="browser" type="O">Madfox</item>
  <item kat="browser" type="O">Maxthon</item>
  <item kat="browser" type="O">Swift</item>
  <item kat="browser" type="O">Voyager</item>
  <item kat="browser" type="O">xB Browser</item>
</products>

Example 42.2. List: browsere.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="da">
    <head>
      <title>Test</title>
      <meta charset="utf-8"/>
    </head>
    <body>
    <h1>Products, ordered list</h1>
    <ol>
      <xsl:apply-templates/>
    </ol>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <li><xsl:apply-templates/></li>
  </xsl:template>
  <xsl:template match="name"/>



</xsl:stylesheet>

Try it!


Example 42.3. Sorted List: browsere1.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>
    <head>
      <title>Test</title>
      <meta charset="utf-8"/>
    </head>
    <body>
    <h1>Products, alphabetically sorted</h1>
    <xsl:apply-templates select="item">
      <xsl:sort/>   <!-- order descending: <xsl:sort order="descending"/> -->
                    <!-- order numerically: <xsl:sort select="@xxx" data-type="number"/> -->
    </xsl:apply-templates>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <p><xsl:apply-templates/></p>
  </xsl:template>
</xsl:stylesheet>

Try it!


Example 42.4. Numbereded List: browsere2.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>
    <head>
      <title>Test</title>
      <meta charset="utf-8"/>
    </head>
    <body>
    <h1>Products, numbered by xslt</h1>
    <xsl:apply-templates select="item">
      <xsl:sort/>
    </xsl:apply-templates>
    <p><i>Total number of items: <xsl:value-of select="count(item)"/></i></p>
    </body>
    </html>
  </xsl:template>

  <xsl:template match="item">
    <p><xsl:number format="1. " value="position()"/><xsl:apply-templates/></p>
  </xsl:template>
</xsl:stylesheet>

Try it!