我有一个以下的XSLT,想在
我尝试着创建一个
我知道我可以使用
你有任何想法吗?
xsl:for-each
元素内使用apply-templates
,这样我就不必重复带有"cliente
" XML元素信息的<tr>
元素。我尝试着创建一个
xsl:template
并将xsl:apply-templates
放在xsl:for-each
内部,但没有成功。我知道我可以使用
xsl:call-template
,但是否有任何方法可以在for-each
内外使用xsl:apply-templates
?你有任何想法吗?
<?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>
<head><title>Informações</title></head>
<body>
<h1>Relação de Clientes</h1>
<table border="2">
<tr bgcolor="LightBlue">
<th>Nome</th>
<th>Telefone</th>
<th>Cidade</th>
<th>Estado</th>
<th>Crédito</th>
</tr>
<tr>
<th colspan="6" bgcolor="LightPink">Critério usado abaixo: exibir todos os elementos ordenado por nome</th>
</tr>
<xsl:for-each select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
<tr>
<td bgcolor="LightGreen"><xsl:value-of select="nome"/></td>
<td><xsl:value-of select="telefone"/></td>
<td><xsl:value-of select="cidade"/></td>
<td><xsl:value-of select="estado"/></td>
<td><xsl:value-of select="credito"/></td>
</tr>
</xsl:for-each>
<tr>
<th colspan="6" bgcolor="LightCyan"> Critério usado abaixo: exibir os clientes da cidade do Rio de Janeiro</th>
</tr>
<xsl:for-each select="informacoes/cliente">
<xsl:if test="cidade='Rio de Janeiro'">
<tr>
<td bgcolor="LightGreen"><xsl:value-of select="nome"/></td>
<td><xsl:value-of select="telefone"/></td>
<td><xsl:value-of select="cidade"/></td>
<td><xsl:value-of select="estado"/></td>
<td><xsl:value-of select="credito"/></td>
</tr>
</xsl:if>
</xsl:for-each>
<tr>
<th colspan="6" bgcolor="LightYellow"> Critério usado abaixo: exibir os clientes do estado do RJ com ordenado pelo nome; </th>
</tr>
<xsl:for-each select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
<xsl:if test="estado='RJ'">
<tr>
<td bgcolor="LightGreen"><xsl:value-of select="nome"/></td>
<td><xsl:value-of select="telefone"/></td>
<td><xsl:value-of select="cidade"/></td>
<td><xsl:value-of select="estado"/></td>
<td><xsl:value-of select="credito"/></td>
</tr>
</xsl:if>
</xsl:for-each>
<tr>
<th colspan="6" bgcolor="LightYellow"> Critério usado abaixo: exibir os clientes com crédito entre 250 e 400, em ordem descendente de crédito) </th>
</tr>
<xsl:for-each select="informacoes/cliente">
<xsl:sort select="credito" order="descending" />
<xsl:if test="credito>250 and credito<400">
<tr>
<td bgcolor="LightGreen"><xsl:value-of select="nome"/></td>
<td><xsl:value-of select="telefone"/></td>
<td><xsl:value-of select="cidade"/></td>
<td><xsl:value-of select="estado"/></td>
<td><xsl:value-of select="credito"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<for-each select=x><apply-templates select="."/></for-each>
是非常不必要的,因为它等效于<apply-templates select=x/>
。 - Michael Kayxsl:for-each
会更简单易行。本来是想回答如何实现的问题,但忘记提供更好、更简单的方法了。 - Mads Hansen