Home > database >  XSLT multiple when conditions with AND
XSLT multiple when conditions with AND

Time:07-03

I want to output a different text when reading the value of two different nodes based on a when condition that I have combined with AND. Only output when both conditions are true.

XML file 1:

<mxc>
    <einvoice>
      <buyer>
        <endpoint>36433875185</endpoint>
        <registeredname>LAV</registeredname>
      </buyer>
    </einvoice>
</mxc>

XML file 2:

<mxc>
    <einvoice>
      <buyer>
        <endpoint>36433875185</endpoint>
        <registeredname></registeredname>
      </buyer>
    </einvoice>
</mxc>

Here is the code I have tried but it always outputs WRONG when I would expect 1310 for XML file 1 and 1170 for XML file 2. I don't know where I'm going wrong.

<xsl:output method="text" />
<xsl:template match="/">
    <xsl:choose>
        <xsl:when test="mxc/einvoice/buyer/registeredname=LAV and mxc/einvoice/buyer/endpoint=36433875185">
            <xsl:text>1310</xsl:text>
        </xsl:when>
        <xsl:when test="mxc/einvoice/buyer/registeredname=' ' and mxc/einvoice/buyer/endpoint=36433875185">
            <xsl:text>1170</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>WRONG</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

CodePudding user response:

Issues and corrections:

  1. The LAV test is testing against an element, not a string. You meant "LAV".

  2. ' ' is a string consisting of a blank character. You meant '', an empty string.

  3. For the first xsl:when/@test, be careful to ensure that registeredname and endpoint have the same heritage:

    mxc/einvoice/buyer[registeredname="LAV" and endpoint="36433875185"]
    

    Adjust your second xsl:when/@test similarly.

  • Related