本文介绍了如何在空手道的 XML 响应中获取标签计数和标签值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的回复如下
<List>
<Fee>
<Amount>10</Amount>
</Fee>
<Fee>
<Amount>10</Amount>
<Amount>20</Amount>
</Fee>
</List>
我想获得每笔费用的金额标签计数.还有每项费用的金额列表.
I would like to get count of Amount tag for each fee. Also list of amounts for each fees.
如何在空手道中实现这一目标.
How to achieve this in karate.
fee1 1 金额标签Fee2 2 个金额标签
fee1 1 amount tagsFee2 2 amount tags
推荐答案
* def response =
"""
<List>
<Fee>
<Amount>10</Amount>
</Fee>
<Fee>
<Amount>10</Amount>
<Amount>20</Amount>
</Fee>
</List>
"""
您可以像这样使用 XPath 获取所有第二个数量的数组:
You can get an array of all the second amounts using XPath like this:
* def amt2 = /List/Fee[2]/Amount
* match karate.sizeOf(amt2) == 2
但在空手道中,返回单个节点的 XPath 将不是数组.但是你可以使用一些条件逻辑:
But in Karate, an XPath that returns a single node will not be an array. But you can use some conditional logic:
* def amt1 = /List/Fee[1]/Amount
* if (karate.typeOf(amt1) != 'list') amt1 = ([amt1])
* match karate.sizeOf(amt1) == 1
以下是编写辅助函数的方法:
Here is how you can write a helper function:
* def fun = function(index){ var temp = karate.xmlPath(response, '/List/Fee[' + index + ']/Amount'); return karate.typeOf(temp) == 'list' ? temp : [temp] }
* match fun(1) == ['10']
* match fun(2) == ['10', '20']
如果上面有任何不清楚的地方,请阅读文档.
And please read the docs if any of the above is not clear.
这篇关于如何在空手道的 XML 响应中获取标签计数和标签值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!