Xslt使用xsl:variable循环

时间:2013-02-17 07:51:13

标签: xslt

我想根据它的计数使用xsl:variable和loop,但我不确定它是否可能在Xslt中。例如,如果我有一个变量名称计数

<xsl:variable name="count" as="xs:integer" select="4"/>

我可以使用以下形式的变量!!!

<xsl:if test="some condition"/>
loop from 0 to $count
...do something here
end loop
</xsl:if>

有可能吗?

我的输入XML:

<Root>
<Element>
    <Value>1</Value>
    <Value>2</Value>
</Element>
<Element>
    <Value>1</Value>
    <Value>2</Value>
    <Value>3</Value>
    <Value>4</Value>
</Element>
    <Element>
    <Value>1</Value>
</Element>
</Root>

平面文件中的预期输出是(带换行符):

1,2,,
1,2,3,4
1,,,

任何帮助表示赞赏。谢谢Mh。

3 个答案:

答案 0 :(得分:7)

使用XSLT 2.0解决方案可能是:

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" />
    <xsl:variable name="count" select="4" />

    <xsl:template match="Element">
        <xsl:value-of select="for $i in 1 to $count return concat(Value[$i], '')"
                      separator="," />
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

    <xsl:template match="text()" />

</xsl:stylesheet>

注意:您也可以使用if语句而不是concat函数。


为了完整起见,使用XSLT 1.0编写的解决方案:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" />

    <xsl:variable name="count" select="4" />

    <!-- Ignore all text elements -->    
    <xsl:template match="text()" />

    <xsl:template match="Element">
        <xsl:if test="$count > 0">
            <!-- Output existing values -->
            <xsl:apply-templates select="Value[position() &lt;= $count]" />
            <!-- Output remaining commas -->
            <xsl:call-template name="print-commas">
                <xsl:with-param name="number"
                                select="$count - count(Value)" />
            </xsl:call-template>            
            <!-- Line break -->
            <xsl:text>&#xA;</xsl:text>
        </xsl:if>
    </xsl:template>

    <!-- Print the first value without a comma preprended to the value -->
    <xsl:template match="Value[1]">
        <xsl:value-of select="." />
    </xsl:template>

    <!-- Print the reamaining value with a comma preprended to the value -->
    <xsl:template match="Value">
        <xsl:value-of select="concat(',', .)" />
    </xsl:template>

    <!-- Print the given amount of commas -->
    <xsl:template name="print-commas">
        <!-- Number of commas to be printed -->
        <xsl:param name="number" />

        <xsl:if test="$number > 0">
            <xsl:text>,</xsl:text>
            <!-- Recursive call decrementing the number of commas to
                 be printed -->
            <xsl:call-template name="print-commas">
                <xsl:with-param name="number"
                                select="$number - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:3)

是的,这在XSLT 2.0中是可行的(从您的示例中的as="xs:integer"开始,我假设您正在使用)。以下转换将从您的示例输入生成预期输出:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="count" select="4"/>

    <xsl:template match="text()" />

    <xsl:template match="Element">
        <xsl:variable name="curElement" select="."/>
        <xsl:for-each select="1 to $count">
            <xsl:variable name="curVal" select="."/>
            <xsl:value-of select="$curElement/Value[. = $curVal]"/>
            <xsl:if test="$curVal != $count">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text>&#xA;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

答案 2 :(得分:2)

示例中的“循环”:

http://www.w3schools.com/xsl/xsl_for_each.asp

您可以“匹配”每个“元素”而无需“for-each”

<xsl:template match="element" >
  <xsl:if test="not(constant(value, ' '))">
     <xsl:value-of select="concat(value/text(), ',')"/>
  <xsl:if>
</xsl:template>
相关问题