如果所有标签都等于值,则 XSLT 返回布尔值 [英] XSLT to return boolean value if all tags are equal to value

查看:27
本文介绍了如果所有标签都等于值,则 XSLT 返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <success>0</success>
    <success>0</success>
    <success>1</success>
</soapenv:Body>

我想实现一个逻辑,如果成功标签之一等于 0,则返回 true,如果它们都等于 0,则返回 false.

And I want to implement logic that will return true if one of the success tags is equal to 0 and false if all of them are equal to 0.

到目前为止我有,但我不知道如何让 xslt 返回 errorFlag=false 如果它们都是 =0:

So far I have but I don't know how to make the xslt to retun errorFlag=false if all of them are =0:

<xsl:template match="/">
  <xsl:call-template name="test" />
</xsl:template>

 <xsl:template match="/soapenv:Envelope/soapenv:Body" name ="test">
   <errorFlag>
     <xsl:if test="contains(.,'0')">true</xsl:if>
   </errorFlag>
</xsl:template>

想要的输出 - 只有一个字段:

Wanted output -only one field:

<errorFlag>true<errorFlag> (if all are success=0)

推荐答案

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:key name="succKey" match="success" use="." />

<xsl:template match="/">
    <xsl:call-template name="test" />
</xsl:template>

<xsl:template match="/soapenv:Envelope/soapenv:Body" name="test">
    <xsl:variable name="key-count"
        select="count(//success[generate-id() =
                               generate-id(key('succKey', .))])" />
    <errorFlag>
        <xsl:choose>
            <xsl:when test="$key-count = 1">
                <xsl:text>true</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>false</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </errorFlag>
</xsl:template>
</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/6r5Gh33

这篇关于如果所有标签都等于值,则 XSLT 返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆