2

I have used regular expression to remove few unwanted items in my XML. Now, I need to print all includedService nodes which contain multiple child elements inside.

Here's code i used;

* string test = read('classpath:PP1/data/Test.xml')
* string test= test.replaceAll("xsi:nil[^/]*", "")
* def Complete_XML = test
* def included = $Complete_XML/Envelope/Body/getPricePlanResponse/pricePlanSummary/includedService
* print included

If i run this, I get below response.

20:19:54.169 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] [
  [#document: null],
  [#document: null]
]

However, I can print selected elements outside of includedService node. Please help!

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header/>
  <S:Body>
    <ns10:getPricePlanResponse>
      <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>
Nitheesh hunsur
  • 151
  • 2
  • 11

1 Answers1

1

Keep in mind XML always has to be wrapped inside some element or the other. Try this:

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

You should get:

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

For other ideas, refer this answer: https://stackoverflow.com/a/53553979/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248