空手道-使用名称空间解析XML [英] Karate - Parsing XML with namespace

查看:65
本文介绍了空手道-使用名称空间解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析并打印 ns4:feature 部分.空手道以json格式打印.我尝试参考答案.但是,我得到了'ERROR:'未声明前缀'xsi'的命名空间.'错误,如果使用建议的xPath.即

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]

这是我的XML :它包含许多具有不同"ns"值的部分,但我在这里给出了Extraxt.

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>

这是我使用的xPath;

注意:我在xml上方将其保存在单独的文件test1.xml中.我只是在读取并解析值.

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

这是我得到的答复;

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

如何在XML中获得相同的内容?

How can i get the same in XML?

推荐答案

这很有趣,我之前从未见过.问题是您有一个带有名称空间xsi:nil="true"的属性,当您使用XML的子集但不再定义该名称空间时,这会引起问题.如果先删除它,一切都会正常.

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.

尝试一下:

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

您可能尝试过的另一种方法是在执行XPath之前执行字符串替换以除去XML中的麻烦内容.

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

添加了有关如何使用Java进行字符串替换的信息.下面将删除整个xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns4:Plan"部分.

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.

这篇关于空手道-使用名称空间解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆