5

我有一个如下所示的 XSLT,并且想apply-templates在元素内部使用,xsl:for-each因此我不必使用“ ” XML 元素<tr>的信息重复该元素。cliente

我正在尝试但没有成功创建一个xsl:templatexsl: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&gt;250 and credito&lt;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>
4

2 回答 2

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&gt;250 and credito&lt;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-templatesselect 语句来进一步简化样式表;xsl:sort在必要时应用应用模板的内部,以确保cliente以所需的顺序处理选定的元素。

<xsl:apply-templates select="informacoes/cliente[estado='RJ']">
  <xsl:sort select="nome" order="ascending" />
</xsl:apply-templates>
于 2012-09-17T00:45:49.200 回答
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&gt;250 and credito&lt;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>
于 2012-09-17T01:42:34.077 回答