I have the following XML:
我有以下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>
and the following XSL:
和以下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>
The XSL will output False twice because the ID I capture is not in the path attribute in the location element as I would like.
XSL将输出False两次,因为我捕获的ID不在我想要的位置元素的path属性中。
How do I count this output, i.e. output the number 2 as the complete result of this XSL?
如何计算此输出,即输出数字2作为此XSL的完整结果?
1 个解决方案
#1
0
You don't need the xsl:for-each here, you can use the function count to count the number of nodes matching a given criteria
您不需要xsl:for-each,您可以使用函数计数来计算与给定条件匹配的节点数
<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>
The expression may be better re-written as <xsl:value-of select="count(mp3[not(contains(location/@path, id))])" />
though, given that contains returns either true or false.
该表达式可以更好地重写为
#1
0
You don't need the xsl:for-each here, you can use the function count to count the number of nodes matching a given criteria
您不需要xsl:for-each,您可以使用函数计数来计算与给定条件匹配的节点数
<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>
The expression may be better re-written as <xsl:value-of select="count(mp3[not(contains(location/@path, id))])" />
though, given that contains returns either true or false.
该表达式可以更好地重写为