del*_*lta 5 xslt foreach templates match apply-templates
我有一个像下面的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>
Run Code Online (Sandbox Code Playgroud)
在您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>
Run Code Online (Sandbox Code Playgroud)
然后,创建模板以匹配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>
Run Code Online (Sandbox Code Playgroud)
您还可以<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>
Run Code Online (Sandbox Code Playgroud)
将这些更改应用于示例样式表:
<?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>
Run Code Online (Sandbox Code Playgroud)
正如Dimitre Novatchev的回答所示,您可以通过消除xsl:for-each语句和调整xsl:apply-templatesselect语句来进一步简化样式表.xsl:sort在必要时应用apply-templates 的内部,以确保cliente以所需顺序处理所选元素.
<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
<xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7211 次 |
| 最近记录: |