Template rules are modules that describe how a particular part of your source XML should be output : 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. 1. Template rules are modules that describe how a particular part of your source XML should be output
A template rule has three parts: 
    the opening tag describes which part(sof your XML document the template should be applied to, 
    the middle bit describes what should happen once a match is found, 
    and the closing tag completes the template.


File: Data.xml

<?xml version="1.0"?>

<python version="2.3">
  <keyword>while</keyword>
  <keyword>continue</keyword>
  <keyword>def</keyword>
  <keyword>elif</keyword>
  <keyword>except</keyword>
  <keyword>from</keyword>

</python>

File: Transform.xslt

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:saxon="http://icl.com/saxon"
  extension-element-prefixes="saxon">
  <xsl:output method="text" encoding="ISO-8859-1" />
  <xsl:strip-space elements="*" />

  <xsl:template match="python">
    <!-- to result tree as text -->
    <xsl:text>Python 2.3 Keywords&#10;&#10;</xsl:text>
    <xsl:apply-templates select="keyword" mode="text">
      <xsl:sort />
    </xsl:apply-templates>
    <!-- save as HTML, too -->
    <saxon:output href="keywords.html" method="html" indent="yes"
      saxon:indent-spaces="1">
      <xsl:fallback />
      <html>
        <body>
          <h3>Python 2.3 Keywords</h3>
          <ol>
            <xsl:apply-templates select="keyword"
              mode="html">
              <xsl:sort />
            </xsl:apply-templates>
          </ol>
        </body>
      </html>
    </saxon:output>
  </xsl:template>

  <xsl:template match="keyword" mode="html">
    <li>
      <xsl:value-of select="." />
    </li>
  </xsl:template>

  <xsl:template match="keyword" mode="text">
    <xsl:value-of select="." />
    <xsl:choose>
      <xsl:when
        test="not((position() mod 5)=0) and not(position()=last())">
        <xsl:text>&#09;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>&#10;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

</xsl:stylesheet>

Output:

Python 2.3 Keywords

continue  def  elif  except  from
while
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.