空手道-输出中有[#document:null]



我使用正则表达式删除了XML中一些不需要的项。现在,我需要打印所有includedService节点,这些节点内部包含多个子元素。

这是我使用的代码;

* 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

如果我运行这个,我会得到以下响应。

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

但是,我可以在includedService节点之外打印选定的元素。请帮忙!

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

请记住,XML总是必须包装在某个元素中。试试这个:

* 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

你应该得到:

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

有关其他想法,请参阅以下答案:https://stackoverflow.com/a/53553979/143475

相关内容

最新更新