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

XML Tutorial
1. Introduction
2. Namespace
3. XML Schema
4. XPath
5. XSLT stylesheet
Java
XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
XML Tutorial » XSLT stylesheet » apply templates 
5. 35. 3. Creating and Applying Template Rules
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.java2java.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.java2java.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
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.