Mega Code Archive

 
Categories / XML Tutorial / XSLT StyleSheet
 

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(s) of 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