2016-03-11 48 views
0

我有一个XSL,我得到6个变量的值。 6个变量有一个整数值,比如3,4等。我需要检查所有6个变量是否具有相同的值。我如何在XSL中实现这一点? 我的XML是:如何检查多个变量在XSL中是否具有相同的值?

<records> 
    <A> 
     <Value> test </Value> 
     <Value> test </Value> 
     <Value> test </Value> 
    </A> 
    <B> 
     <Value> test </Value> 
    </B> 
    <C> 
     <Value> test </Value> 
     <Value> test </Value> 
    </C> 
    <D> 
     <Value> test </Value> 
    </D> 
    <E> 
     <Value> test </Value> 
     <Value> test </Value> 
    </E> 
    <F> 
     <Value> test </Value>       
    </F> 
</records> 

我的XSL是这样的:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

    <xsl:variable 
    name="noOfANodes" 
    select="count(/records/A/*)"> 
    </xsl:variable> 

    <xsl:variable 
    name="noOfBNodes" 
    select="count(/records/B/*)"> 
    </xsl:variable> 

    <xsl:variable 
    name="noOfCNodes" 
    select="count(/records/C/*)"> 
    </xsl:variable> 

    <xsl:variable 
    name="noOfDNodes" 
    select="count(/records/D/*)"> 
    </xsl:variable> 

    <xsl:variable 
    name="noOfENodes" 
    select="count(/records/E/*)"> 
    </xsl:variable> 

    <xsl:variable 
    name="noOfFNodes" 
    select="count(/records/F/*)"> 
    </xsl:variable> 

    <xsl:template match="/"> 
     <xsl:choose> 
      <!-- The following is obv wrong. --> 
      <xsl:when test="$noOfANodes=$noOfBNodes=$noOfCNodes=$noOfDNodes"> 
     </xsl:when> 

    <xsl:otherwise> 

    </xsl:otherwise> 

    </xsl:choose> 
    </xsl:template> 

回答

2

在xpath中,您将多个测试与关键字and结合在一起。

在你的情况,你可以重写你的测试作为

<xsl:when test="$noOfANodes=$noOfBNodes and $noOfBNodes=$noOfCNodes and $noOfCNodes=$noOfDNodes and $noOfDNodes=$noOfENodes and $noOfENodes=$noOfFNodes"> 
    <!-- Do something here --> 
</xsl:when> 

这里我们测试配对 - 测试第一变量对第二个,然后测试对第三第二(我们并不需要测试第一次对第三次,因为我们知道如果这个测试是真的,那么这是真的),然后测试第三次对第四次,等等。


如果你可以使用XSLT2,这甚至可以一定程度上,我们建立了一系列的每个值的除了最后一个,并检查它们各自对最后简化为

<xsl:when test="every $m in ($noOfANodes,$noOfBNodes,$noOfCNodes,$noOfDNodes,$noOfENodes) satisfies ($m=$noOfFNodes)"> 
    <!-- Do something here --> 
</xsl:when> 

。如果他们都与最后一样,我们知道他们都是平等的。

我们实际上可以在XSLT1的情况下使用这个相同的逻辑,并对每个变量进行最后的测试。

+0

XSLT 2是XSLT的一个单独的版本?我如何使用它? – Chandeep

+0

@Chandeep你必须有一个XSLT2处理器。撒克逊是最广泛使用的开源软件之一。否则,只需要在样式表上设置版本(您已将版本设置为2.0)并理解两个版本之间的差异即可。 XSLT2增加了一些功能,并且有一些功能的工作方式不同。 – Matthew

+0

谢谢!这有帮助 – Chandeep

1

应使用复杂的情况是这样的:

<xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 

<xsl:variable 
name="noOfANodes" 
select="count(/records/A/*)"> 
</xsl:variable> 

<xsl:variable 
name="noOfBNodes" 
select="count(/records/B/*)"> 
</xsl:variable> 

<xsl:variable 
name="noOfCNodes" 
select="count(/records/C/*)"> 
</xsl:variable> 

<xsl:variable 
name="noOfDNodes" 
select="count(/records/D/*)"> 
</xsl:variable> 

<xsl:variable 
name="noOfENodes" 
select="count(/records/E/*)"> 
</xsl:variable> 

<xsl:variable 
name="noOfFNodes" 
select="count(/records/F/*)"> 
</xsl:variable> 

<xsl:template match="/"> 
    <xsl:choose> 
     <!-- The following can extend to any number of statements. --> 
     <xsl:when test="$noOfANodes=$noOfBNodes and $noOfANodes=$noOfCNodes and $noOfANodes=$noOfDNodes"> 
    </xsl:when> 

<xsl:otherwise> 

</xsl:otherwise> 

</xsl:choose> 
</xsl:template> 

希望这有助于...

1

在XSLT 2.0,你可以写

count(distinct-values(($noOfANodes, $noOfBNodes, $noOfCNodes, 
$noOfDNodes, $noOfENodes, $noOfFNodes))) = 1 

更妙的是,避免使用六个变量:

count(distinct-values(/records/*/count(Value))) = 1 
相关问题