0

在下面的 XSL 中,每次 xsl:when 得到满足时,我都想附加尽可能多的<a> 和</a> 标记。但是需要在标签内填充的数据应该只有一次。我在最后展示了预期的输出。

<xsl:param name="insert-file" as="document-node()" />
<xsl:template match="*">
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
            <xsl:with-param name="nodeValue" select="$input"/> 
    </xsl:call-template> 
</xsl:variable>
<xsl:copy-of select="$Myxml"></xsl:copy-of>
</xsl:template>

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:for-each select="$insert-file/insert-data/data">
        <xsl:choose> 
            <xsl:when test="@index = 1">
                <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
            </xsl:when>             
        </xsl:choose> 
    </xsl:for-each> 
</xsl:template>     

电流输出:

<?xml version="1.0" encoding="UTF-8"?> <a>我的文字</a> <a>我的文字</a> <a>我的文字</a> <a>我的文字</a>

我希望模板“populateTag”以以下格式向我返回 xml。我如何修改模板“populateTag”以达到相同的效果。

模板“populateTag”的预期输出: <?xml version="1.0" encoding="UTF-8"?> <a> <a> <a> <a>我的文本</a> </a> </a> </a>

请给出你的想法。

4

1 回答 1

1

为此,您需要某种递归(嵌套 a 元素)。

无需尝试,因为我没有示例 XML 文档:

<xsl:param name="insert-file" as="document-node()" />
<xsl:template match="*">
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
            <xsl:with-param name="nodeValue" select="$input"/> 
            <xsl:with-param name="position" select="1"/> 
    </xsl:call-template> 
</xsl:variable>
<xsl:copy-of select="$Myxml"></xsl:copy-of>
</xsl:template>

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:param name="position"/> 
    <xsl:variable name="total" select="count($insert-file/insert-data/data[@index = 1])" />
    <xsl:for-each select="$insert-file/insert-data/data[@index = 1]">
      <xsl:if test="position() = $position" >
          <xsl:choose> 
              <xsl:when test="position() = $total">
                  <a><xsl:value-of select="$nodeValue"></xsl:value-of></a>
              </xsl:when>             
              <xsl:otherwise>
                <a>    
                      <xsl:call-template name="populateTag"> 
                              <xsl:with-param name="nodeValue" select="$input"/> 
                              <xsl:with-param name="position" select="$position+1"/> 
                      </xsl:call-template> 
                </a>
              </xsl:otherwise>
          </xsl:choose> 
        </xsl:if>
    </xsl:for-each> 
</xsl:template>
于 2010-04-23T18:15:55.600 回答