apply-templates select="county" mode="county" : apply templates « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<us>
 <state name="Hawaii">
  <county name="Hawaii">
   <city class="largest">Hilo</city>
  </county>
 </state>
</us>



File: Transform.xslt

<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />

  <xsl:template match="us/state">
    <html>
      <head>
        <title>
          State:
          <xsl:value-of select="@name" />
        </title>
      </head>
      <body>
        <h1>
          State:
          <xsl:value-of select="@name" />
        </h1>
        <h2>All Counties</h2>
        <ul>
          <xsl:apply-templates select="county" mode="county" />
        </ul>
        <h2>Largest Cities (by County)</h2>
        <ul>
          <xsl:apply-templates select="county" mode="city" />
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="county" mode="county">
    <li>
      <xsl:value-of select="@name" />
    </li>
  </xsl:template>

  <xsl:template match="county" mode="city">
    <li>
      <xsl:value-of select="city" />
      (
      <xsl:value-of select="@name" />
      )
    </li>
  </xsl:template>

</xsl:stylesheet>

Output:


<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>
                   State:
                   Hawaii
      </title></head>
   <body>
      <h1>
                   State:
                   Hawaii
      </h1>
      <h2>All Counties</h2>
      <ul>
         <li>Hawaii</li>
      </ul>
      <h2>Largest Cities (by County)</h2>
      <ul>
         <li>Hilo
                  (
                  Hawaii
                  )
                
         </li>
      </ul>
   </body>
</html>








5.35.apply templates
5.35.1.Template rules are modules that describe how a particular part of your source XML should be output
5.35.2.apply-templates select="county" mode="county"
5.35.3.Creating and Applying Template Rules