0

次の XML があります。

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="count-example.xsl"?>
<musiclist>
<mp3>
    <id>MP1003</id>
    <artist>Frank Sinatra</artist>
    <title>Fly Me To The Moon</title>
    <location path="home/music/sinatra/MP1008.mp3" />
</mp3>
<mp3>
    <id>MP1004</id>
    <artist>Frank Sinatra</artist>
    <title>New York, New York</title>
    <location path="home/music/sinatra/MP1004.mp3" />
</mp3>
<mp3>
    <id>MP1005</id>
    <artist>Frank Sinatra</artist>
    <title>Young At Heart</title>
    <location path="home/music/sinatra/MP1009.mp3" />
</mp3>
</musiclist>

および次の XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="musiclist">
    <xsl:for-each select="mp3">
        <xsl:variable name="idvar" select="id" />
        <xsl:if test="contains(location/@path, $idvar) = 0">
            false
        </xsl:if>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

XSL は False を 2 回出力します。これは、取得した ID が location 要素のパス属性に含まれていないためです。

この出力をカウントするにはどうすればよいですか。つまり、この XSL の完全な結果として数値 2 を出力しますか?

4

1 に答える 1

0

ここではxsl:for-eachは必要ありません。関数countを使用して、特定の基準に一致するノードの数をカウントできます。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="musiclist">
      <xsl:value-of select="count(mp3[contains(location/@path, id) = 0])" />
   </xsl:template>
</xsl:stylesheet>

式は、 containsが true または false を返す<xsl:value-of select="count(mp3[not(contains(location/@path, id))])" />ことを考えると、より適切に書き直すことができます。

于 2013-10-11T07:03:26.570 に答える