XSLT to return boolean value if all tags are equal to value

前端 未结 3 969
礼貌的吻别
礼貌的吻别 2021-01-28 22:22

I have a xml:



    0
             


        
相关标签:
3条回答
  • 2021-01-28 23:04

    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.

    First, let's make the truth table:

    All zeros | None zero | Some zero, others not
    -------------------------------------------
      False   |   False   |        True
    

    Second, node-set comparison in XPath are existencial. So:

    boolean(/soapenv:Envelope/soapenv:Body[success = 0 and success != 0])
    

    It will return true or false boolean value.

    This input

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

    With this stylesheet

    <xsl:stylesheet 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" version="1.0">
       <xsl:template match="/">
          <xsl:value-of 
             select="boolean(
                        /soapenv:Envelope
                           /soapenv:Body[
                              success = 0 and success != 0
                           ]
                     )"/>
       </xsl:template>
    </xsl:stylesheet>
    

    Returns

    true
    

    Check it in http://www.utilities-online.info/xsltransformation/?save=ceed7a50-6a47-4c2c-b344-86ad3b3b0d92-xsltransformation

    0 讨论(0)
  • 2021-01-28 23:10

    How about:

    <xsl:template match="/">
        <errorFlag>
             <xsl:value-of select="not(/soapenv:Envelope/soapenv:Body/success=1)" />
        </errorFlag>
    </xsl:template>
    

    Or (requires XSLT 2.0):

    <xsl:template match="/">
        <errorFlag>
            <xsl:value-of select="every $item in /soapenv:Envelope/soapenv:Body/success satisfies $item=0"/>
        </errorFlag>
    </xsl:template>
    
    0 讨论(0)
  • 2021-01-28 23:13
    <?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

    0 讨论(0)
提交回复
热议问题