Karate - Parsing XML with namespace

橙三吉。 提交于 2020-01-05 08:54:10

问题


I need to parse and print ns4:feature part. Karate prints it in json format. I tried referring to this answer. But, i get 'ERROR: 'Namespace for prefix 'xsi' has not been declared.' error, if used suggested xPath. i.e.,

* def list = $Test1/Envelope/Body/getPlan/planSummary/feature[1]

This is my XML: It contains lot many parts with different 'ns' values, but i have given here an extraxt.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header/>
   <S:Body>
      <ns9:getPlan xmlns:ns10="http://xmlschema.test.com/xsd_v8" xmlns:ns9="http://xmlschema.test.com/srv/SMO_v4" xmlns:ns8="http://xmlschema.test.com/xsd/Customer_v2" xmlns:ns7="http://xmlschema.test.com/xsd/Customer/Customer_v4" xmlns:ns6="http://schemas.test.com/eca/common_types_2_1" xmlns:ns5="http://xmlschema.test.com/xsd/Customer/BaseTypes_1_0" xmlns:ns4="http://xmlschema.test.com/xsd_v4" xmlns:ns3="http://xmlschema.test.com/xsd/Enterprise/BaseTypes/types/ping_v1" xmlns:ns2="http://xmlschema.test.com/xsd/common/exceptions/Exceptions_v1_0">
        <ns9:planSummary xsi:type="ns4:Plan" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <ns5:code>XPBSMWAT</ns5:code>
           <ns5:description>Test Plan</ns5:description>
           <ns4:category xsi:nil="true"/>
           <ns4:effectiveDate>2009-11-05</ns4:effectiveDate>
           <ns4:sharingGroupList>
              <ns4:sharingCode>CAD_DATA</ns4:sharingCode>
              <ns4:contributingInd>true</ns4:contributingInd>
           </ns4:sharingGroupList>
           <ns4:feature>
              <ns5:code>ABC</ns5:code>
              <ns5:description>Service</ns5:description>
              <ns5:descriptionFrench>Service</ns5:descriptionFrench>
              <ns4:poolGroupId xsi:nil="true"/>
              <ns4:switchCode/>
              <ns4:type/>
              <ns4:dtInd>false</ns4:dtInd>
              <ns4:usageCharge>0.0</ns4:usageCharge>
              <ns4:connectInd>false</ns4:connectInd>
           </ns4:feature>
        </ns9:planSummary>
      </ns9:getPlan>
   </S:Body>
</S:Envelope>

This is the xPath i used;

Note: I saved above xml in a separate file test1.xml. I am just reading it and parsing the value.

* def Test1 = read('classpath:PP1/data/test1.xml')
* def list = $Test1/Envelope/Body/*[local-name()='getPlan']/*[local-name()='planSummary']/*[local-name()='feature']/*
* print list

This is the response i am getting;

16:20:10.729 [ForkJoinPool-1-worker-1] INFO  com.intuit.karate - [print] [
  "ABC",
  "Service",
  "Service",
  "",
  "",
  "",
  "false",
  "0.0",
  "false"
]

How can i get the same in XML?


回答1:


This is interesting, I haven't seen this before. The problem was you have an attribute with a namespace xsi:nil="true" which is causing problems when you take a sub-set of the XML but the namespace is not defined anymore. If you remove it first, things will work.

Try this:

* remove Test1 //poolGroupId/@nil
* def temp = $Test1/Envelope/Body/getPlan/planSummary/feature

Another approach you could have tried is to do a string replace to remove troublesome stuff in the XML before doing XPath.

EDIT: added info on how to do a string replace using Java. The below will strip out the entire xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Plan" part.

* string temp = Test1
* string temp = temp.replaceAll("xmlns:xsi[^>]*", "")
* print temp

So you get the idea. Just use regex.



来源:https://stackoverflow.com/questions/53681930/karate-parsing-xml-with-namespace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!