0

次の xml があるとします。

<parameterGroup>
    <parameter value="1" name="Level0_stratum">
    </parameter>
    <parameter value="1" name="Level2_stratum">
    </parameter>
    <parameter value="1" name="Level1_stratum">
    </parameter>
    <parameter value="6" name="foo">
    </parameter>       
    <parameter value="9" name="bar">
    </parameter>    
</parameterGroup>

すべての Level*_stratum 値の @value が同じかどうかを示すブール変数を導出したいと思います。この場合は (1) です。

これまでのところ、次のように、関連するすべてのノードをセットにグループ化できました。

select="//parameter[starts-with(@name,'Level') and ends-with(@name,'_stratum') ]"

しかし、すべての @value 属性が等しいかどうかを比較する最も効率的な方法がわかりませんか?

4

2 に答える 2

2

end-with() が利用可能な場合、XSLT 2.0 を使用しているため、distinct-values() が利用可能であるため、簡単に実行できます。

count(distinct-values(
  //parameter[starts-with(@name,'Level') and ends-with(@name,'_stratum') ])/@value))
= 1
于 2013-02-04T14:41:51.997 に答える
1

私はこれがあなたがしようとしていることをするはずだと信じています (value-of行は必要ではなく、変数の値を表示するためだけにあります):

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

    <xsl:template match="/">
      <xsl:variable 
             name="allStrata"
             select="//parameter[starts-with(@name, 'Level') and
                                 ends-with(@name, '_stratum')]" />
      <xsl:value-of select="concat(count($allStrata), ' strata. ')"/>

      <!-- Determines whether all strata have the same values by comparing them all 
             against the first one. -->
      <xsl:variable name="allStrataEqual" 
                    select="not($allStrata[not(@value = $allStrata[1]/@value)])" />

      <xsl:value-of select="concat('All equal: ', $allStrataEqual)" />
    </xsl:template>
</xsl:stylesheet>

上記のサンプル入力でこれを実行すると、結果は次のようになります。

3 strata. All equal: true

value3 番目を 8 (またはそれ以外) に変更した後、サンプル入力でこれを実行すると、結果は次のようになります。

3 strata. All equal: false
于 2013-02-04T03:30:44.693 に答える