1

I have two xml files I want to compare with a match. But I want to use embedded expressions / only define what I'm looking for in the expected xml file. I don't want to write specific match for this scenario as I want to have a large scenario outline with many xml files that can be totally unique.

In my example, I'm interested in only matching Item1 and not any other items. The list can be out of order. How can I achieve this?

My expected xml:

<Something>
    <Items>
        <Item1 Code="123">
            <SubItem></SubItem>
        </Item1>
        #ignore
    </Items>
</Something>

My actual xml:

<Something>
    <Items>
        <Item2 Code="123">
            <SubItem></SubItem>
        </Item2>
        <Item1 Code="123">
            <SubItem></SubItem>
        </Item1>
    </Items>
</Something>

EDIT:

I would like to only use fuzzy matching.

I see a case like #[] bar.bar in other examples, referencing a def in the karate scenario. But I would like to do #[] <Item1></<Item1> so that everything is self contained in the XML file, so that the structure I want to match against is still defined in the xml.

My expected XML would look like this then:

<Something>
    <Items>
        #[] <Item1 Code="123">
            <SubItem></SubItem>
        </Item1>
    </Items>
</Something>

Where I would hope I could define something like this which says, match that Items is an array/collection of elements and Items contains this specific element.

#[] <Item1 Code="123">
        <SubItem></SubItem>
    </Item1>
visc
  • 4,794
  • 6
  • 32
  • 58

1 Answers1

0

Maybe you are overcomplicated things. This below will work:

* def response =
"""
<Something>
    <Items>
        <Item2 Code="123">
            <SubItem></SubItem>
        </Item2>
        <Item1 Code="123">
            <SubItem></SubItem>
        </Item1>
    </Items>
</Something>
"""
* def item1 = /Something/Items/Item1
* match item1 == <Item1 Code="123"><SubItem></SubItem></Item1>

You can even do it in one line:

* match /Something/Items/Item1 == <Item1 Code="123"><SubItem></SubItem></Item1>

If you have a more complicated case, please ask a new question.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I want to exclusively use fuzzy matching in XML. Is this possible? Can this be possible? It would ideal if I can keep all my matching inside the XML files exclusively for my use case. – visc Feb 15 '22 at 13:37
  • I added an edit to the question. Thank you – visc Feb 15 '22 at 13:39
  • @visc it may be possible if you convert to JSON or refer to the XML examples in the documentation. I'm sorry I can't help more than this. I personally also disagree that you need a "more generic" way to do this, it causes more trouble than its worth in my experience, for e.g: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Feb 15 '22 at 13:40
  • Except that I have many many permutations. Being able to match xml is important and isn't "over engineering" as your opinion believes. Instead of referring to karate, I would like to put xml directly in a fuzzy match expression. Is that something that can be added to karate? – visc Feb 15 '22 at 14:49
  • And not to sound complaining or over dramatic- it just feels like XML is a second class citizen. There seems to be a lot more focus on json. When in practice there also seems to still be a lot of SOAP xml in enterprise – visc Feb 15 '22 at 14:51
  • 1
    @visc I will the first to agree / admit that XML support is not as good as JSON, this is technical - for e.g. in JSON there is no concept of a `root` element. things go downhill from there. for some ideas on how to workaround, read this (and the links onward): https://stackoverflow.com/a/58543843/143475 - and of course contributions and pull-requests are highly welcome – Peter Thomas Feb 15 '22 at 15:29