空手道-XML-打印子节点 [英] Karate - XML - Printing Child node
问题描述
我需要打印XML的某个子节点,该节点具有多个子节点.我在下面给出了一个示例xml.但是,空手道会转换为json并以json格式打印数据;但我需要将其以XML格式返回.
I need to print a certain child node of my XML which is having multiple child nodes. I have given a sample xml below. But, karate converts to json and prints the data in json format; but i need it to be returned in 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查询.
This is the xpath query i am using to print second SubTest node with code = 567.
* def PP_XML = $xml/root/Test/SubTest[2]/*
* print PP_XML
这给出了这样的响应;
This gives response like this;
13:40:19.391 [ForkJoinPool-1-worker-1] INFO com.intuit.karate - [print] [
"567",
"Your Description",
"BH"
]
有什么方法可以隔离并仅以正确的XML打印第二个子SubTest节点?
Is there any way i can isolate and print only second child SubTest node in proper XML?
我也尝试过以下一项;但没有运气.
I tried with below one too; but without luck.
* def PP_XML = $xml/root/Test/SubTest[2]
* print PP_XML
我还尝试了消除过程,方法是排除其他两个子节点,如下所示;这会引发无法转换为xml"错误.我知道这是空手道处理xml的方式,但想知道我是否可以重现它.
I tried elimination process as well, by excluding other 2 child nodes like below; this throws 'cannot convert to xml' error. I know this is how karate handles xml but wanna know if i can reproduce this.
* 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]
* print PP_XML
赠予:
15:21:24.723 [main] INFO com.intuit.karate - [print] <ns5:SubTest>
<ns6:code>567</ns6:code>
<ns6:description>Your Description</ns6:description>
<ns6:categoryCode>BH</ns6:categoryCode>
</ns5:SubTest>
似乎可以正常工作.
这篇关于空手道-XML-打印子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!