空手道xpath-我们可以替换/删除节点 [英] karate xpath - Can we replace/delete a node

查看:54
本文介绍了空手道xpath-我们可以替换/删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过修改有效的xml消息文件来测试错误消息.在这种情况下,我可以轻松添加这样的节点:

I'm looking to test error messages by modifying a valid xml message file. I can easily add nodes like this in the scenario:

* def invalidDocumentId = read('Valid.xml')*设置invalidDocumentId/soapenv:Envelope/soapenv:Body =< tis:extraBonus> Extra</tis:extraBonus>给定请求invalidDocumentId

但是我也想删除节点,更新节点名称,更改属性名称.有没有办法设置该值,或者我需要调用Java com.intuit.karate.XmlUtils.

But I'd like to also remove nodes, update node names, change attribute names. Is there a way to set that or do I need to call Java com.intuit.karate.XmlUtils.

推荐答案

首先,删除和添加节点甚至属性都应该很容易,只需使用 remove set 使用XPath的关键字.如果需要,它们将覆盖值.

First, removing and adding a node and even attributes should be easy, just use the remove and set keywords with XPath. They will over-write values if needed.

* def base = <query><name>foo</name></query>
* remove base /query/name
* match base == <query/>
* set base /query/foo = 'bar'
* set base /query/@baz = 'ban'
* match base == <query baz="ban"><foo>bar</foo></query>

好消息是,如果您确实有一些真正棘手的XML操作要求,则可以使用字符串 replace 语法.最好在堆栈溢出的其他答案中对此进行解释: https://stackoverflow.com/a/50367134/143475 | https://stackoverflow.com/a/53682733/143475

And the good news is that if you have some really tricky XML manipulation requirements, the string replace syntax comes to the rescue. This is best explained in this other answer on Stack Overflow: https://stackoverflow.com/a/50367134/143475 | https://stackoverflow.com/a/53682733/143475

这篇关于空手道xpath-我们可以替换/删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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