如何在XSLT 1.0中使用test测试多个字符串

时间:2015-07-28 19:53:15

标签: xslt xpath conditional-statements

我正在尝试测试多个字符串的条件(基本上我正在测试错误代码)

我已经通过Dimitre Novatchev URL

但仍未取得丰硕成果。

目前我正在做这件事并且正在努力。

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

我试过这个

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

但它没有帮助。

有人可以指点我如何在一个条件下测试相同的东西吗?

输入请求:

 <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>IVERTRK@HOTMAIL.COM</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>

单个条件测试工作代码

<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>

添加更多非工作代码:

<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>

$rc的值只不过是我从后端迈克尔得到的响应代码。值总是动态的,如500,404 Not Found等,

希望这能澄清它。

1 个答案:

答案 0 :(得分:0)

如果您只是想检查数字响应代码,则需要删除rc中的所有字母:<xsl:variable name="nc" select="translate($rc, translate($rc, '0123456789',''), '')"/><xsl:when test="contains('|500|404|403|204|',concat('|', $nc ,'|'))">