很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .
9年前关闭。
我有一个像下面这样的 XSLT,想使用 apply-templates
内xsl:for-each
元素,所以我不必重复 <tr>
具有“cliente
” XML 元素信息的元素。
我正在尝试但没有成功创建 xsl:template
并放 xsl:apply-templates
内xsl:for-each
.
我知道我可以使用 xsl:call-template
,但是有什么方法可以使用 xsl:apply-templates
内外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" />
<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>
最佳答案
您的内部 xsl:for-each
您正在迭代的地方 informacoes/cliente
,上下文节点将是当前 cliente
元素。
为了apply-templates
对于上下文节点,您可以使用 .
在您的选择语句中。 例如:
<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
选择语句;申请 xsl:sort
必要时在应用模板内部确保选择 cliente
元素按所需顺序处理。<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
关于templates - XSLT 在 for-each 中应用模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451533/