给定以下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值的@值是否相同,就像本例中的(1)一样。
到目前为止,我已经能够将所有相关节点分组,如下所示:
select="//parameter[starts-with(@name,'Level') and ends-with(@name,'_stratum') ]"但我不确定比较所有@value属性是否相等的最有效方法?
发布于 2013-02-04 11:30:45
我相信这应该能完成您想要做的事情( 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如果在将第三个value更改为8(或其他任何值)后对示例输入运行此命令,则结果为:
3 strata. All equal: false发布于 2013-02-04 22:41:52
如果ends-with()可用,那么您正在使用XSLT2.0,因此distinct-values()可用,因此您可以简单地执行以下操作
count(distinct-values(
//parameter[starts-with(@name,'Level') and ends-with(@name,'_stratum') ])/@value))
= 1https://stackoverflow.com/questions/14679681
复制相似问题