lucianthomaz
lucianthomaz

Reputation: 195

Validate missing tag xQuery

I'm using xQuery for an automated test with SoapUI and I'm facing some issue when trying to validate if an Tag exists. I have the following XML:

<Results>
    <ResultSet fetchSize="10">
        <Row rowNumber="1">
            <FCTAAPLI>2309516299-0</FCTAAPLI>
            <FDATA>2017-05-05 00:00:00</FDATA>
            <FVALOR>0.02</FVALOR>
        </Row>
        <Row rowNumber="2">
            <FCTAAPLI>2312518455-1</FCTAAPLI>
            <FDATA>2017-05-05 00:00:00</FDATA>
            <FVALOR>0.54</FVALOR>
        </Row>
    </ResultSet>
</Results>

I'm trying to use the following code:

<Results>
{
for $z in //ResultSet/Row
where $z/FCTAAPLI = "${Properties#NUM_APLICACAO}"

return if (string($z) = "")
then 0
else
number($z/FVALOR)
}
</Results>

EDIT: What I'm trying to do is: Loop through the Row elements, searching for a specific FCTAAPLI number. If that number is found, then the value on the FVALOR tag should be returned. That point works fine.
The problem is that, when no element match the desired FCTAAPLI number, I would like to return an 0. But, as no element is returned, I'm having some trouble on validating that.

When the FCTAAPLI number is found, everything works fine, the FVALOR is returned, but when FCTAAPLI isn't found, instead of it returning 0, it simply returns the tag [< Results/ >]. I've tried using empty($z) and exist($z) but it didn't work either. Any idea on how could I solve this?

Upvotes: 0

Views: 264

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

Your "where" condition is saying that you're only interested in rows where $z/FCTAAPLI = "${Properties#NUM_APLICACAO}", but your description of the problem suggests that you also want output in cases where that's not the case.

I've no idea what the thinking behind string($z) = "" is. None of the rows in your example data have a zero-length string value, so I've no idea what this condition was intended to achieve.

At this point I'm realising that you don't really say what you want your query to do. You say it works under some conditions and doesn't work under other conditions, but it's hard to correct it without knowing what the "correct answer" is supposed to be.

Perhaps you intended something like:

<Results>
{
for $z in //ResultSet/Row
return if $z/FCTAAPLI = "${Properties#NUM_APLICACAO}"
       then number($z/FVALOR)
       else 0
}
</Results>

but that's a pretty wild guess.

== LATER ==

With your revised statement of requirements, it looks like you simply want

(//ResultSet/Row
   [FCTAAPLI = "${Properties#NUM_APLICACAO}"]/number($z/FVALOR), 0)[1]

The idiom

(a/b/c, $default)[1]

looks strange on first encounter but it's very useful: it returns the first item selected by a/b/c if there is one, or $default otherwise.

Upvotes: 5

Related Questions