Nitheesh hunsur
Nitheesh hunsur

Reputation: 161

karate.set - Output doesn't contain all xml nodes

I have to parse pricePlanSummary elements excluding includedService child node. I tried with the suggested karate.set method. But my output contains only one xml element.

My Code

* def included = $Test1/Envelope/Body/getPricePlanResponse/pricePlanSummary/*[not(self::includedService)]
* def root = <root></root>
* def fun = function(x, i){ karate.set('root', '/pricePlanSummary[' + (i + 1) + ']', x) }
* eval karate.forEach(included, fun)
* print root

If I run this for below XML, i get <root>0</root> in the response. Looks like i need to parse few [#document: null] elements first and then parse entire output. I don't understand how to do this. This is very similar to the last question i asked; but i am stuck here again.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header/>
  <S:Body>
    <ns10:getPricePlanResponse>
    <ns4:dataSharingGroupCode>CAD_DATA</ns4:dataSharingGroupCode>
      <ns10:pricePlanSummary>
        <ns5:descriptionFrench>Forfait Montre Affaires</ns5:descriptionFrench>
        <ns4:category/>
        <ns4:effectiveDate>2009-11-05</ns4:effectiveDate>
        <ns4:serviceDataSharingGroupList>
          <ns4:dataSharingGroupCode>CAD_DATA</ns4:dataSharingGroupCode>
          <ns4:contributingInd>true</ns4:contributingInd>
        </ns4:serviceDataSharingGroupList>
        <ns4:feature>
          <ns5:descriptionFrench>Service</ns5:descriptionFrench>
          <ns4:poolGroupId/>
        </ns4:feature>
        <ns4:recurringCharge>10.0</ns4:recurringCharge>
        <ns4:ppsStorageSize>0</ns4:ppsStorageSize>
        <ns4:includedService>
          <ns4:term>0</ns4:term>
          <ns4:brandId>1</ns4:brandId>
          <ns4:feature>
            <ns5:code>MBAPN</ns5:code>
            <ns4:type/>
            <ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
          </ns4:feature>
          <ns4:recurringCharge>0.0</ns4:recurringCharge>
          <ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
        </ns4:includedService>
        <ns4:includedService>
          <ns4:term>0</ns4:term>
          <ns4:brandId>1</ns4:brandId>
          <ns4:feature>
            <ns5:code>MBAPN</ns5:code>
            <ns4:type/>
            <ns4:additionalNumberRequiredInd>false</ns4:additionalNumberRequiredInd>
          </ns4:feature>
          <ns4:recurringCharge>0.0</ns4:recurringCharge>
          <ns4:callingCircleFeaturesInd>false</ns4:callingCircleFeaturesInd>
        </ns4:includedService>
        <ns4:availableTermInMonths>0</ns4:availableTermInMonths>
      </ns10:pricePlanSummary>
    </ns10:getPricePlanResponse>
  </S:Body>
</S:Envelope>

Upvotes: 1

Views: 230

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

It is easier to remove all the includedService elements.

* def included = $Test1/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService
* def fun = function(){ karate.remove('Test1', '/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService') }
* eval karate.forEach(included, fun)
* print Test1 

Personally I think you seem to be doing un-necessary things. Are you using Karate to process XML or just validate expected results ? Please think hard about this question. You typically never need to create XML using Karate. For assertions, just check that the XML contains some elements and you are done.

Upvotes: 1

Related Questions