XML:模糊匹配带来麻烦 [英] XML : trouble with fuzzy matching

查看:45
本文介绍了XML:模糊匹配带来麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML匹配方面遇到了麻烦,它的工作方式似乎与JSON有所不同.

I am having troubles with XML matching, which appears to be working a bit differently from JSON.

我找到了此代码段

* def xml = <foo><bar>baz</bar></foo>
* set xml/foo/bar = <hello>world</hello>
* match xml == <foo><bar><hello>world</hello></bar></foo>

但是,我不能指定我正在使用模板,并且<hello>world</hello>可能出现多次.

But with this, I cannot specify that I'm using a template, and that <hello>world</hello> may be present more than once.

方案XML 1发生故障,而其他XML仍在工作.

Scenario XML 1 is failing, while the others are working.

Scenario: Scenario XML 1

    * def response = <response><foo><bar><msg name="Hello"/><msg name="World"/></bar><bar><msg name="Hello"/><msg name="World"/></bar></foo></response>
    * def bar = <bar><msg name="Hello"/><msg name="World"/></bar>
    * def foo = <response><foo>#[](bar)</foo></response>
    * print foo
    * print response
    * match response == foo

Scenario: Scenario XML 2

    * def response = <response><foo><bar><msg name="Hello"/><msg name="World"/></bar></foo></response>
    * def bar = <bar><msg name="Hello"/><msg name="World"/></bar>
    * def foo = <response><foo>#(bar)</foo></response>
    * print foo
    * print response
    * match response == foo

Scenario: Scenario JSON 1
    * def response = {"response": {"foo": [{"bar": [{"msg": "Hello World"},{"msg": "Hello World"}]}, {"bar": [{"msg": "Hello World"},{"msg": "Hello World"}]}]}}
    * def bar = {"bar": [{"msg": "Hello World"},{"msg": "Hello World"}]}
    * def foo = {"response": {"foo": #[](bar)}}
    * print foo
    * print response
    * match response == foo

Scenario: Scenario JSON 2
    * def response = {"response": {"foo": {"bar": [{"msg": "Hello World"},{"msg": "Hello World"}]}}}
    * def bar = {"bar": [{"msg": "Hello World"},{"msg": "Hello World"}]}
    * def foo = {"response": {"foo": #(bar)}}
    * print foo
    * print response
    * match response == foo

如何使场景XML 1工作?

How can I have Scenario XML 1 working?

推荐答案

我承认这可以视为一个空白. XML重复元素与JSON如此不同的事实无济于事.我能做的最好的是:

I admit this can be considered a gap. The fact that XML repeated elements are so different from JSON doesn't help. The best I could do is this:

* def response = <foo><bar><msg name="Hello"/><msg name="World"/></bar><bar><msg name="Hello"/><msg name="World"/></bar></foo>
* def bar = <bar><msg name="Hello"/><msg name="World"/></bar>
* def foo = <foo>#ignore</foo>
* match response == foo
* match /foo/bar/msg[1]/@name == ['Hello', 'Hello']
* def names = $response/foo/bar/msg[1]/@name
* match each names == 'Hello'

随时提交功能请求,并根据您对JSON的经验,建议理想的语法是什么样.

Feel free to submit a feature request and suggest based on your experience with JSON what the ideal syntax should look like.

经过一点思考并意识到,由于Karate如何在内部将XML转换为类似JSON的数据,您可以使用此选项.

thought about it a little and realized because of how Karate converts XML into JSON-like data internally, you have this option.

* json response = <response><foo><bar><msg name="Hello"/><msg name="World"/></bar><bar><msg name="Hello"/><msg name="World"/></bar></foo></response>
* json bar = <bar><msg name="Hello"/><msg name="World"/></bar>
* match each response.response.foo.bar == bar.bar
* match response == { response: { foo: { bar: '#[] bar.bar' } } }

我知道这可能有点难以理解,但是会起作用的:)我现在正在看代码,并且由于JSON匹配涉及的原因-不太可能被重构以支持重复的XML元素.

I know it may be a little hard to understand, but will work :) I was looking at the code right now, and because of how involved the JSON matching is - it is unlikely to get re-factored to support XML repeated elements.

实际上,我们现在已经进行了修复,因此也应该可行:

actually we made a fix now so this should be possible also:

* match response == <response><foo><bar>#[] bar.bar</bar></foo></response>

https://github.com/intuit/karate/issues/653

这篇关于XML:模糊匹配带来麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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