我有一个类似下面的XSLT,并希望在apply-templates
元素中使用xsl:for-each
,因此我不必使用“{{的信息重复<tr>
元素1}}“XML元素。
我正在尝试但没有成功创建cliente
并将xsl:template
放入xsl:apply-templates
。
我知道我可以使用xsl:for-each
,但有没有办法在xsl:call-template
内外使用xsl:apply-templates
?
关于如何做到这一点的任何想法?
for-each
答案 0 :(得分:7)
在您xsl:for-each
迭代的informacoes/cliente
内,上下文节点将是当前的cliente
元素。
为了apply-templates
上下文节点,您可以在select语句中使用.
。例如:
<xsl:for-each select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
<xsl:apply-templates select="."/>
</xsl:for-each>
然后,创建模板以匹配cliente
元素:
<xsl:template match="informacoes/cliente">
<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:template>
您还可以通过使用<xsl:if>
轴引用当前上下文节点,然后在谓词过滤器中应用测试条件来消除围绕某些项目的self::
测试。上下文节点:
<xsl:for-each select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
<xsl:apply-templates select="self::*[estado='RJ']"/>
</xsl:for-each>
将这些更改应用于示例样式表:
<?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" />
<xsl:apply-templates select="."/>
</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:apply-templates select="self::*[cidade='Rio de Janeiro']"/>
</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:apply-templates select="self::*[estado='RJ']"/>
</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:apply-templates select="self::*[credito>250 and credito<400]"/>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="informacoes/cliente">
<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:template>
</xsl:stylesheet>
正如Dimitre Novatchev的回答所示,您可以通过删除xsl:for-each
语句并调整xsl:apply-templates
选择语句来进一步简化样式表。必要时在apply-templates内部应用xsl:sort
,以确保按所需顺序处理选定的cliente
元素。
<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
答案 1 :(得分:3)
只需替换:
<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>
。通过强>:
<xsl:apply-templates select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
同样,替换:
<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>
<强>与强>:
<xsl:apply-templates select="informacoes/cliente[cidade='Rio de Janeiro']"/>
同样,替换:
<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>
<强>与强>
<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
最后替换:
<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>
<强>与强>:
<xsl:apply-templates select="informacoes/cliente[credito >250 and 400 > credito]">
<xsl:sort select="credito" order="descending" />
</xsl:apply-templates>
然后添加此简单模板:
<xsl:template match="informacoes/cliente">
<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:template>
您的完整XSLT代码现已成为:
<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:apply-templates select="informacoes/cliente">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
<tr>
<th colspan="6" bgcolor="LightCyan"> Critério usado abaixo: exibir os clientes da cidade do Rio de Janeiro</th>
</tr>
<xsl:apply-templates select="informacoes/cliente[cidade='Rio de Janeiro']"/>
<tr>
<th colspan="6" bgcolor="LightYellow"> Critério usado abaixo: exibir os clientes do estado do RJ com ordenado pelo nome; </th>
</tr>
<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
<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:apply-templates select="informacoes/cliente[credito >250 and 400 > credito]">
<xsl:sort select="credito" order="descending" />
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="informacoes/cliente">
<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:template>
</xsl:stylesheet>