xslt来自循环的单个输出

时间:2013-12-04 12:59:57

标签: loops xslt

我有一个看起来像这样的源文件

<company>
    <ids>
        <id provider="AAA">123456</id>
    </ids>
    <businessDetails>
        <blocks>
            <block channel="google.com"/>
            <block channel="nokia.com"/>
            <block channel="bing.com"/>
        </blocks>
        <registration>
            <registrationNumber>1352045555</registrationNumber>
            <registrationDate>1983-09-17</registrationDate>
        </registration>
    </businessDetails>
</company>

我需要遍历块以查看是否应该发送注册号码。我的xslt的一部分看起来像这样

<xsl:choose>
<xsl:when test="s2:businessDetails/s2:blocks">
    <xsl:for-each select="s2:businessDetails/s2:blocks/s2:block">
        <xsl:variable name="var:v1" select="userCSharp:LogicalEq(string(./@channel=&quot;google.com&quot;) , &quot;true&quot;)"/>
        <xsl:variable name="var:v2" select="userCSharp:LogicalEq(string(./@channel=&quot;bing.com&quot;) , &quot;true&quot;)"/>
        <xsl:choose>
            <xsl:when test="($var:v1 or $var:v2)='true'">
                                </xsl:when>
            <xsl:otherwise>
                <ns0:companyOrgNumber>
                    <xsl:value-of select="../../s2:registration/s2:registrationNumber/text()"/>
                </ns0:companyOrgNumber>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>
</xsl:when>
<xsl:otherwise>
    <xsl:if test="s2:businessDetails/s2:registration/s2:registrationNumber">
        <ns0:companyOrgNumber>
            <xsl:value-of select="s2:businessDetails/s2:registration/s2:registrationNumber/text()"/>
        </ns0:companyOrgNumber>
    </xsl:if>
</xsl:otherwise>
</xsl:choose>

问题是当我有块时,但它们都不匹配var:v1或var:v2我在目标文件中得到了几个。如果所有块都是假的,我只想拥有一个。

1 个答案:

答案 0 :(得分:0)

看起来你应该这样做:

<xsl:if test="s2:businessDetails/s2:blocks[not(s2:block[@channel='google.com']) and not(s2:block[@channel='bing.com'])]">
    <ns0:companyOrgNumber>
        <xsl:value-of select="s2:businessDetails/s2:registration/s2:registrationNumber/text()"/>
    </ns0:companyOrgNumber>
</xsl:if>

你真的不需要遍历它 - 只需检查是否缺少这些节点。