Elemento <xsl:template> do XSLT

An XSL style sheet is composed of one or more sets of rules called templates (template).

Each template contains the rules to be applied when a specified node is matched.

The <xsl:template> element

The <xsl:template> element is used to build templates.

match Attributes are used to associate XML elements with templates. The match attribute can also be used to define a template for the entire document. The value of the match attribute is an XPath expression (for example, match="/" defines the entire document).

Alright, let's take a look at the simplified version of the XSL file from the previous section:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
 <html>
 <body>
   <h2>My CD Collection</h2>
   <table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
       <td>.</td>
       <td>.</td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

Code Explanation:

Since the XSL style sheet itself is also an XML document, it always starts with an XML declaration:

<?xml version="1.0" encoding="ISO-8859-1"?>

O próximo elemento,<xsl:stylesheet>Define este documento como um documento de folha de estilo XSLT (juntamente com o número da versão e a propriedade do espaço de nomes XSLT).

<xsl:template> O elemento define um modelo. E match="/" Os atributos conectam este modelo ao documento de origem XML raiz.

O conteúdo interno do elemento <xsl:template> define o código HTML escrito no resultado da saída.

As últimas duas linhas definem o final do modelo e o final da folha de estilo.

O resultado da conversão acima é semelhante a este:

Ver Arquivo XML,Ver Arquivo XSL,Ver Resultado

O resultado deste exemplo tem um pequeno defeito, pois os dados não foram copiados do documento XML para a saída.

Na próxima seção, você aprenderá a usar <xsl:value-of> O elemento选取值来自XML元素。