templates - XSLT 在 for-each 中应用模板

标签 templates xslt foreach match apply-templates

很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center .




9年前关闭。




我有一个像下面这样的 XSLT,想使用 apply-templatesxsl:for-each元素,所以我不必重复 <tr>具有“cliente ” XML 元素信息的元素。

我正在尝试但没有成功创建 xsl:template并放 xsl:apply-templatesxsl: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>

最佳答案

您的内部 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&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-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/

相关文章:

c++ - 在C++中复制动态分配的数组

c++ - 关键字 'template' 混淆了 MSVC

xml - 如何使用 XSLT 从 XML 中删除 namespace

java - 用于 Java 的 XSLT 框架

c# - 从预设位置开始迭代,到达终点后继续迭代

c++ - `type` 和 `const type` 的类型特征

php - Smarty 可以只读取一个 block 而不是模板吗?

xslt - 使用 XSLT 变量保存属性的问题

php - 检查 mySQL 表中是否存在数组项的最快方法

java - Foreach in foreach 从列表中删除项目