我如何使用XSLT重复X次动作

ipa*_*aus 14 xml forms xslt

我要用XSLT填充总共20个元素.在我的XML代码中,我有一个<select>带有值的,无论如何都要不写20个表单?

我的XML:

<output>
    <select>
        <id>1</id>
        <name>One</name>
    </select>
    <select>
        <id>2</id>
        <name>Two</name>
    </select>
    <select>
        <id>3</id>
        <name>Three</name>
    </select>
    <!-- An more -->
</output>
Run Code Online (Sandbox Code Playgroud)

我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <xsl:template match="/">
        <html>

            <body>
        <select name="values[]">
            <option value="0"> </option>
            <xsl:for-each select="output/select">
                <option>
                    <xsl:attribute name="value"><xsl:value-of select="id"></xsl:attribute>
                    <xsl:value-of select="name" />
                </option>
            </xsl:for-each>
        </select>
            </body>

        </html>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

期望的输出:

<html>
    <body>

        <select name="values[]">
            <option value="0"> </option>
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </select>
        <!-- But 20 times -->   

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ode*_*ded 12

首先,而不是使用的模板for-each,那么你可以使用一个递归调用模板来模拟一个for循环(如看到这里):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   
    <xsl:template match="/">
        <html>
        <body>

            <xsl:call-template name="selects">
               <xsl:with-param name="i">1</xsl:with-param>
               <xsl:with-param name="count">20</xsl:with-param>
            </xsl:call-template> 

        </body>    
        </html>
    </xsl:template>

    <xsl:template name="selects">
        <xsl:param name="i" />
        <xsl:param name="count" />

        <xsl:if test="$i &lt;= $count">
          <select name="values[]">
            <xsl:apply-template select="output/select" />
          </select>
        </xsl:if>

        <!--begin_: RepeatTheLoopUntilFinished-->
        <xsl:if test="$i &lt;= $count">
            <xsl:call-template name="selects">
                <xsl:with-param name="i">
                    <xsl:value-of select="$i + 1"/>
                </xsl:with-param>
                <xsl:with-param name="count">
                    <xsl:value-of select="$count"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:if>

    </xsl:template>

    <xsl:template match="output/select">
      <option>
        <xsl:attribute name="value">
            <xsl:value-of select="id">
        </xsl:attribute>
        <xsl:value-of select="name" />
      </option>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)


Dim*_*hev 5

一,XSLT 1.0解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <html>
    <body>
      <xsl:apply-templates select="*" mode="iter">
       <xsl:with-param name="pCount" select="20"/>
      </xsl:apply-templates>
    </body>
  </html>
 </xsl:template>

 <xsl:template match="/*" mode="iter">
  <xsl:param name="pCount" select="0"/>

  <xsl:if test="$pCount > 0">
      <select name="values[]">
      <xsl:apply-templates/>
      </select>
    <xsl:apply-templates select="." mode="iter">
      <xsl:with-param name="pCount" select="$pCount -1"/>
    </xsl:apply-templates>
  </xsl:if>
 </xsl:template>

 <xsl:template match="select">
  <option value="{id}"><xsl:value-of select="name"/></option>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这是一个特定的递归解决方案。

当应用于以下XML文档时

<output>
    <select>
        <id>0</id>
        <name> </name>
    </select>
    <select>
        <id>1</id>
        <name>One</name>
    </select>
    <select>
        <id>2</id>
        <name>Two</name>
    </select>
    <select>
        <id>3</id>
        <name>Three</name>
    </select>
</output>
Run Code Online (Sandbox Code Playgroud)

产生想要的正确结果

二。使用f:repeat()FXSL功能的XSLT 2.0解决方案

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="f xs"
 >
 <xsl:import href="../f/func-repeat.xsl"/>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:variable name="vSelects" as="element()">
   <select name="values[]">
    <xsl:apply-templates select="/*/select"/>
   </select>
 </xsl:variable>

  <xsl:template match="/">
    <html>
      <body>
       <xsl:sequence select="f:repeat($vSelects, 20)"/>
      </body>
    </html>
  </xsl:template>

 <xsl:template match="select">
  <option value="{id}"><xsl:value-of select="name"/></option>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

在这里,我们使用非常通用的函数,它将重复其第一个参数N(第二个参数的值)的次数。

该函数f:repeat()本身非常简单

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="xs f"
>

  <xsl:function name="f:repeat" as="item()+">
    <xsl:param name="pThis" as="item()"/>
    <xsl:param name="pTimes" as="xs:integer"/>

    <xsl:for-each select="1 to $pTimes">
      <xsl:sequence select="$pThis"/>
    </xsl:for-each>
  </xsl:function>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)