What makes an XPath String?

Questions about XML that are not covered by the other forums should go here.
Stefan_E
Posts: 18
Joined: Sat Nov 07, 2009 12:03 am

What makes an XPath String?

Post by Stefan_E » Sun Nov 08, 2009 2:34 pm

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

sorin_ristache
Posts: 4144
Joined: Fri Mar 28, 2003 2:12 pm

Re: What makes an XPath String?

Post by sorin_ristache » Mon Nov 09, 2009 6:43 pm

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

Stefan_E
Posts: 18
Joined: Sat Nov 07, 2009 12:03 am

Re: What makes an XPath String?

Post by Stefan_E » Mon Nov 09, 2009 10:25 pm

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

Post Reply