tanvi
tanvi

Reputation: 997

Xquery function to compare two xpaths of two different xmls

I am trying to write a function that takes the location of two xml files and an xpath and returns true if the two xpaths have same values. I am using the deep-equal function for the same. When I pass the values in the deep-function function, I am getting the correct answer. However, when I am trying to get the values in a local function, I am always getting a true. I might be taking the parameters wrongly.

Please help with what I am doing wrong here?

This gives the correct value:

deep-equal(doc("C:/xml/1/abc.xml")/root/messageHeader/system, doc("C:/xml/2/abc.xml")/root/messageHeader/system)

This gives true always:

declare function local:findDiff($first as xs:string, $second as xs:string, $xpath as node()*){

let $doc1 := (doc($first))/$xpath
let $doc2 := (doc($second))/$xpath

return deep-equal($doc1, $doc2)};

<findDiff>{local:findDiff("C:/xml/1/abc.xml", "C:/xml/2/abc.xml", root/messageHeader/system)}</findDiff>

Upvotes: 1

Views: 183

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

In your example $xpath is a string. It's the same string regardless whether doc1 or doc2 is the context node. You're therefore comparing the string with itself.

What you want to do is to treat the contents of the string $xpath as an XPath expression, and evaluate that expression. There's no function in XQuery to do that, but many implementations have some kind of extension function for the purpose, usually called something like xx:eval or xx:evaluate where xx refers to a vendor-defined namespace.

Upvotes: 1

Related Questions