Page 1 of 1

What makes an XPath String?

Posted: Sun Nov 08, 2009 2:34 pm
by Stefan_E
I'm a newbie to XQuery and XPath. I'm trying to compare two XML structures. The problem I now have: What exactly makes an XPath (string?)? :?

I have the following code:

Code: Select all

declare variable $x1 := <top>
<rec>
<d1>25</d1>
<d2>10</d2>
</rec>
</top>;
declare variable $x2 := <top>
<rec>
<d1>30</d1>
<d2>10</d2>
</rec>
</top>;
for $node in $x1/rec
return for $elem in $node/*
return if ($elem = $x2/rec/*[local-name()=local-name($elem)])
then (<match>{$elem}</match>)
else (<diff>{$elem}</diff>)
which produces

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<diff>
<d1>25</d1>
</diff>
<match>
<d2>10</d2>
</match>
So far so good, but it took me a while to get there. Specifically, in my original trials, I had something like

Code: Select all

       return if ($elem = $x2/rec/local-name($elem))
then (<match>{$elem}</match>)
else (<diff>{$elem}</diff>)
What confuses me:
  • Checking with the debugger, I find local-name(elem) = 'd1'
  • but also $x2/rec/local-name($elem) evaluates to 'd1' (rather than what I expected: 30
Hence, what I picture for myself as an XPath string seems to be the wrong concept. But what is the right one?

Thanks for your help! Stefan

Re: What makes an XPath String?

Posted: Mon Nov 09, 2009 6:43 pm
by sorin_ristache
Hello,
Stefan_E wrote:I'm a newbie to XQuery and XPath. I'm trying to compare two XML structures. The problem I now have: What exactly makes an XPath (string?)? :?
Maybe you should go through an XPath tutorial?
Stefan_E wrote:What confuses me:
  • Checking with the debugger, I find local-name(elem) = 'd1'
  • but also $x2/rec/local-name($elem) evaluates to 'd1' (rather than what I expected: 30
Hence, what I picture for myself as an XPath string seems to be the wrong concept. But what is the right one?
The result of an expression like $x2/rec/local-name($elem) is the result of the function local-name() which is the element name: d1 or d2 or rec or top etc.


Regards,
Sorin

Re: What makes an XPath String?

Posted: Mon Nov 09, 2009 10:25 pm
by Stefan_E
Hi Sorin,

thanks for the link to the tutorial; I'm also using the O'Reilly book on XQuery, but I lack the explanation of the concept:
  • my background is perl, so I could expect local-name() to evaluate to d1 and then the whole path string to read <evaluation of $x2> . '/rec/' . <evaluation of local-name>, hence /top/rec/d1, which would then again be taken as an XPath string, leading to the element <d1>30</d1>
  • or, as I observe, I only get <evaluation of local-name>; but then I'd expect that somebody throws an error (or at least a warning) because of the unused part $x2/rec/
... so, I'm still a bit searching the concept behind the behavior - in order to make my next XQuery writing considerably faster :wink:

Regards, Stefan