4

カウントに基づいて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。

4

3 に答える 3

8

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>

注: concat 関数の代わりに if ステートメントを使用することもできます。


完全を期すために、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>
于 2013-02-17T09:04:43.313 に答える
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>
于 2013-02-17T08:13:29.957 に答える
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>
于 2013-02-17T09:49:32.037 に答える