Kumar_y
Kumar_y

Reputation: 239

how to test multiple strings using test in XSLT 1.0

I am trying to test the condition for multiple strings ( basically I am testing for the error codes)

I have gone through Dimitre Novatchev post URL

but still no fruitful result.

Currently I am doing this and its working.

<xsl:when test="contains($rc,'500')">
<xsl:when test="contains($rc,'404')">

I have tried this this

<xsl:when test="contains('|500|404|403|204|',concat('|', $rc ,'|'))">

but it didn't helped.

Can anyone please point me how to test the same in one condition?

Input Request:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:agdata:agcelerate:schemas:de-message:inbound:grower:v1">
   <soapenv:Header/>
   <soapenv:Body>
      <msg:growerMessages xmlns:msg="urn:agdata:agcelerate:schemas:de-message:grower:outbound:v1">
         <growerMessage traceNumber="78ca8f3e-f61a-4f22-a634-2fce43e775e7" messageCreated="2014-03-01T14:00:06.3583305-05:00" action="update">
            <growerId>67962</growerId>
            <isRegistered>true</isRegistered>
            <status>active</status>
            <lastFourSSN>2667</lastFourSSN>
            <email>[email protected]</email>
            <name>
               <title>Mr</title>
               <first>THAYNE</first>
               <last>IVERSON</last>
            </name>
            <addresses>
               <physical>
                  <address1>17683 250TH ST</address1>
                  <city>FERGUS FALLS</city>
                  <state>MN</state>
                  <postalCode>56537</postalCode>
                  <country>US</country>
                  <latitude>46.31940</latitude>
                  <longitude>-95.65700</longitude>
                  <deliverableStatus>DLV</deliverableStatus>
               </physical>
               <mailing>
                  <address1>17683 250TH ST</address1>
                  <city>FERGUS FALLS</city>
                  <state>MN</state>
                  <postalCode>56537</postalCode>
                  <country>US</country>
                  <latitude>46.31940</latitude>
                  <longitude>-95.65700</longitude>
                  <deliverableStatus>DLV</deliverableStatus>
               </mailing>
            </addresses>
            <phoneNumbers>
               <office>2187367174</office>
            </phoneNumbers>
            <crops>
               <crop cropId="3" name="Soybeans"/>
            </crops>
            <farms>
               <farm>
                  <farmId>67963</farmId>
                  <lastFourTaxId>2378</lastFourTaxId>
                  <businessName>THAYNE IVERSON</businessName>
                  <role roleId="11" name="FARM BUSINESS"/>
                  <addresses>
                     <physical>
                        <address1>17683 250TH ST</address1>
                        <city>FERGUS FALLS</city>
                        <state>MN</state>
                        <postalCode>56537-7444</postalCode>
                        <country>US</country>
                        <latitude>46.31940</latitude>
                        <longitude>-95.65700</longitude>
                        <deliverableStatus>UND</deliverableStatus>
                     </physical>
                  </addresses>
                  <absentees/>
               </farm>
            </farms>
            <suppliers>
               <supplier>
                  <supplierId>42396</supplierId>
                  <businessName>NEW HORIZONS AG SERVICES</businessName>
                  <addresses>
                     <physical>
                        <address1>22327 138TH AVE</address1>
                        <city>FERGUS FALLS</city>
                        <state>MN</state>
                        <postalCode>56537-7123</postalCode>
                        <country>US</country>
                        <latitude>46.31940</latitude>
                        <longitude>-95.65700</longitude>
                        <deliverableStatus>DLV</deliverableStatus>
                     </physical>
                  </addresses>
                  <phoneNumbers/>
               </supplier>
            </suppliers>
            <license action="new" source="agcelerate">
               <token>bb8dae6b-2a20-46a7-90e1-8339e95be5c8</token>
               <number>50207846</number>
               <effective>2014-03-01T13:26:15.053</effective>
               <signerTitle>OWNER/OPERATOR</signerTitle>
            </license>
         </growerMessage>
      </msg:growerMessages>
   </soapenv:Body>
</soapenv:Envelope>

Single conditional test working code

<xsl:when test="contains($rc,'500')">
                <!--<xsl:when test="contains('|500|404|403|204|',concat('|', $rc ,'|'))">-->
                <dp:set-variable name="'var://service/error-protocol-response'" value="'200'"/>
                <dp:set-variable name="'var://service/error-protocol-reason-phrase'" value="'OK'"/>
            </xsl:when>
            <xsl:when test="contains($rc,'404')">
                <dp:set-variable name="'var://service/error-protocol-response'" value="'200'"/>
                <dp:set-variable name="'var://service/error-protocol-reason-phrase'" value="'OK'"/>
                -->
            <!--                <dp:set-http-response-header name="'x-dp-response-code'" value="'200 OK'"/>
                <dp:append-response-header  name="'x-dp-response-code'" value="'200 OK'"/>
-->
            </xsl:when>

Adding more non working code:

<xsl:variable name="rc" select="normalize-space(dp:response-header('x-dp-response-code'))"/>
    <xsl:message dp:priority="debug">Backend HTTP status = <xsl:value-of select="$rc"/>
    </xsl:message>
    <xsl:choose>
        <!--<xsl:when test="contains($rc,'500')">-->
        <xsl:when test="contains('|500|404|403|204|',concat('|', $rc ,'|'))">

            <dp:set-variable name="'var://service/error-protocol-response'" value="'200'"/>
            <dp:set-variable name="'var://service/error-protocol-reason-phrase'" value="'OK'"/>

        </xsl:when>

The values of $rc is nothing but the response-code I get from the backend Michael. The value is always dynamic like 500, 404 Not Found etc etc,

Hope this Clarifies it.

Upvotes: 0

Views: 175

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

If you simply want to check for the numerical response code then you need to remove all letters from rc: <xsl:variable name="nc" select="translate($rc, translate($rc, '0123456789',''), '')"/> and <xsl:when test="contains('|500|404|403|204|',concat('|', $nc ,'|'))">

Upvotes: 0

Related Questions