【发布时间】:2018-12-07 08:43:57
【问题描述】:
我需要打印具有多个子节点的 XML 的某个子节点。我在下面给出了一个示例 xml。但是,空手道转换为 json 并以 json 格式打印数据;但我需要它以 XML 格式返回。
XML:
* def xml =
"""
<ns4:root xmlns:ns4="http://foo.com" xmlns:ns5="http://bar.com">
<ns4:Test>
<ns5:code>123</ns5:code>
<ns5:description>My Description</ns5:description>
<ns5:SubTest>
<ns6:code>345</ns6:code>
<ns6:description>Your Description</ns6:description>
<ns6:categoryCode>BH</ns6:categoryCode>
</ns5:SubTest>
<ns5:SubTest>
<ns6:code>567</ns6:code>
<ns6:description>Your Description</ns6:description>
<ns6:categoryCode>BH</ns6:categoryCode>
</ns5:SubTest>
<ns5:SubTest>
<ns6:code>784</ns6:code>
<ns6:description>Your Description</ns6:description>
<ns6:categoryCode>BH</ns6:categoryCode>
</ns5:SubTest>
<ns5:categoryCode>DUDU</ns5:categoryCode>
<ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
</ns4:Test>
<ns4:Test>
<ns5:code>675</ns5:code>
<ns5:description>His Description</ns5:description>
<ns5:unwanted>Unwanted XML Parameter</ns5:unwanted>
</ns4:Test>
</ns4:root>
"""
这是我用来打印代码 = 567 的第二个 SubTest 节点的 xpath 查询。
* def PP_XML = $xml/root/Test/SubTest[2]/*
* print PP_XML
这会给出这样的响应;
13:40:19.391 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] [
"567",
"Your Description",
"BH"
]
有什么方法可以隔离并以正确的 XML 仅打印第二个子 SubTest 节点?
我也尝试过以下一种;但没有运气。
* def PP_XML = $xml/root/Test/SubTest[2]
* print PP_XML
我也尝试了消除过程,排除其他 2 个子节点,如下所示;这会引发“无法转换为 xml”错误。我知道这是空手道处理 xml 的方式,但我想知道我是否可以重现它。
* def PP_XML = $xml/root/Test/*[not(self::SubTest[1]) or (self::SubTest[3])]
* xml PP = PP_XML
* print PP
【问题讨论】:
-
* def PP_XML = $xml/root/Test/SubTest[2] 这应该可以工作
-
嘿,sandeep - 请有礼貌地提供格式良好的 XML - 由于剪切和粘贴,您至少有 5 个拼写错误
-
@sandeep 修改了你的 XML