XQuery: saxon:evaluate with relative xpath

Questions about XML that are not covered by the other forums should go here.
Hisham
Posts: 2
Joined: Fri Dec 24, 2010 8:49 am

XQuery: saxon:evaluate with relative xpath

Post by Hisham »

Hi

Can I run dynamic RELATIVE xpath using saxon:evaluate? I retrieve book elements from books.xml file and I want to navigate these elements using relative xpaths and saxon:evaluate. It seems saxon:evaluate understands only absolute paths that starts from the root element.

I am using saxon processor in oXygen.

The following is sample xquery and data files, my application is different.
XQuery:
<Authors>
{
(: Step One I get the dynamic path from paths.xml file :)
let $author_path := data(doc('paths.xml')/Xpaths/Author_Path)
for $elem2 in doc('Books.xml')/Books/Book (:Get book elements:)
return $elem2/saxon:evaluate($author_path) (: Want to navigate a book element here but it does not work:)
}
</Authors>
------------------------------------------------------------
Data file books.xml
<Books>
<Book>
<author>John</author>
<Title>XQuery</Title>
</Book>
<Book>
<author>Tim</author>
<Title>XSLT</Title>
</Book>
</Books>
---------------------------------------------------------------------------------------------
File that contains xpaths that will be loaded and run dynamically
paths.xml
<Xpaths>
<Author_Path> /author[1] </Author_Path>
</Xpaths>
--------------------------------------------------------------------------------------------

The query returns nothing , when I use static path return $elem2/author[1]
it works. When I change the path stored in paths.xml to /Books/Book/author[1] it returns all authors in each iteration.
<Authors>
<author>John</author>
<author>Tim</author>
<author>John</author>
<author>Tim</author>
</Authors>

I appreciate any help

Thank you
Hisham
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XQuery: saxon:evaluate with relative xpath

Post by adrian »

Hello,

It works fine if you keep the XPaths relative in the paths.xml file:
Don't use "/author[1]", that's an absolute XPath, use "author[1]" instead.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Hisham
Posts: 2
Joined: Fri Dec 24, 2010 8:49 am

Re: XQuery: saxon:evaluate with relative xpath

Post by Hisham »

Thanks a lot. I did a silly mistake.

Hisham
Post Reply