Creating and Applying Template Rules : apply templates « XSLT stylesheet « XML Tutorial






File: Data.xml

 
<?xml version="1.0"?>
<employees>
  <animal>
    <name language="English">T1</name>
    <name language="Latin">T2</name>
    <projects>
      <project>project1</project>
    </projects>
  </animal>
</employees>

File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:template match="/">
    <html>
      <head>
        <title>this is the title</title>
      </head>
      <body bgcolor="white">
        <xsl:apply-templates select="employees/animal" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="animal">
    <p align="center">
      <br />
      <font size="+3">
        <xsl:apply-templates select="name" />
      </font>
    </p>
    <paragraph>
      <xsl:value-of select="name[@language='English']" />
      <a href="http://www.java2s.com">pages</a>
    </p>
    <hr />
  </xsl:template>

  <xsl:template match="name[@language='English']">
    <nobr>
      <b>
        <xsl:value-of select="." />
        :
      </b>
    </nobr>
  </xsl:template>

  <xsl:template match="name[@language='Latin']">
    <nobr>
      <i>
        <xsl:value-of select="." />
      </i>
    </nobr>
  </xsl:template>
</xsl:stylesheet>

Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>this is the title</title>
   </head>
   <body bgcolor="white">
      <p align="center"><br><font size="+3">
            <nobr><b>T1
                          :
                        </b></nobr>
            <nobr><i>T2</i></nobr></font></p>
      <paragraph>T1<a href="http://www.java2s.com">pages</a></p>
      <hr>
   </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