Reputation: 6940
I have written a REST service hitting a MySQL database. This service will soon be ported to a newer system using a MongoDB database. Later, it may be ported to another system using Oracle.
Why we are porting so much is unimportant.
There are several nested tags in the resulting XML with the same name, a common case:
Sample:
<root>
<level1>
<name>Karl</name>
</level1>
<level1>
<name>Anton</name>
</level1>
<level1>
<name>Friedrich</name>
</level1>
<level1>
<name>Anton</name>
</level1>
</root>
I have been using SoapUI to test the results between systems, to make sure that the queries and such are still working properly. Integration testing and all that.
So the question is, is there a way in XPath or XQuery to test that "Karl" appears once in the results, "Anton" appears twice, and "Friedrich" appears once? I would normally just test //root/level1[1]/name == "Karl", etc., but I can't rely on consistent ordering between the systems. Is there a way to test unordered results? Or perhaps is there a way to verify that the right number of level1 elements have the same name value within? I know there's a count
function, but I'm unsure how elaborately it can be used.
Upvotes: 1
Views: 434
Reputation: 243529
So the question is, is there a way in XPath or XQuery to test that "Karl" appears once in the results, "Anton" appears twice, and "Friedrich" appears once?
Use:
count(/*/level1/name[.='Karl']) = 1
count(/*/level1/name[.='Anton']) = 2
count(/*/level1/name[.='Friedrich']) = 1
These are three separate XPath expressions, each of which must evaluate to true()
.
Alternatively, you can combine them into a single XPath expression that can be evaluated just once:
count(/*/level1/name[.='Karl']) = 1
and
count(/*/level1/name[.='Anton']) = 2
and
count(/*/level1/name[.='Friedrich']) = 1
Upvotes: 4