XPath 2.0 expression from XQuery?

B

Bloody Viking

Namaste, Y'all!

I've got a valid XQuery expression that I need to convert to XPath
2.0. This expression will be stored in a resource file and applied to
XML by a Java program with saxon8.jar (I'm pretty sure it's v.8.7).
The manifest from the jar file follows:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.5.0_06-b05 (Sun Microsystems Inc.)
Project-Name: Saxon-B
Main-Class: net.sf.saxon.Transform

The question is, how can I convert this to an XPath 2.0 expression?
When I tried simply removing the <XTRACT> tags and outer curly-braces,
the Java 1.6 compiler informed me that "let" is not allowed in XPath.
Removing all these let's is daunting, if not impossible. And, once
I've done that, what other error messages will I encounter?

Is there a better way to do this?

Here's the XQuery:

<XTRACT>
{
let $bodyPosition := 3
for $r in doc("xtractorTest.xml")//BODY/descendant-or-self::*
let $node := concat(upper-case(string(node-name($r))),': ')
let $text := normalize-space(string($r/text()[1]))
let $subnodes := $r/*
let $attrs :=
for $a in $r/@*
return concat( $node, upper-case(string(node-name($a))),': ',$r/
data($a),'
')
let $node := string-join(for $n in $r/ancestor-or-self::*[position()
< last() - $bodyPosition]
return concat(upper-case(string(node-name($n))),': '),'' )
return
if (string-length($text) > 0) then concat($attrs, $node, $text,'
')
else $
attrs
}
</XTRACT>

What this does: for all sub-elements of <BODY>, it generates a string
that is something like breadcrumbs from the names of the elements and,
if appropriate, attribute, colon-delimited, followed by the text.

A brief example:
<BODY>
<Version Number="1"/>
<GEOGRAPHY Region="Middle East" altRegion="Near East">
<Sub-Region>Palestine</Sub-Region>
<Country>Israel
<LOCATION>West Bank<town>Bethlehem</town></LOCATION>
<LOCATION>Gaza Strip</LOCATION></Country>
</GEOGRAPHY>
<TOPIC language="English">INTERNATIONAL POLITICAL</TOPIC>
<TOPIC language="Hebrew">LEADER</TOPIC>
</BODY>

The XQuery generates:
VERSION: NUMBER: 1
GEOGRAPHY: REGION: Middle East
GEOGRAPHY: ALTREGION: Near East
GEOGRAPHY: SUB-REGION: Palestine
GEOGRAPHY: COUNTRY: Israel
GEOGRAPHY: COUNTRY: LOCATION: West Bank
GEOGRAPHY: COUNTRY: LOCATION: TOWN: Bethlehem
GEOGRAPHY: COUNTRY: LOCATION: Gaza Strip
TOPIC: LANGUAGE: English
TOPIC: INTERNATIONAL POLITICAL
TOPIC: LANGUAGE: Hebrew
TOPIC: LEADER

TIA,

Paul M Lieberman
 
J

Joseph Kesselman

XQuery can often be converted to XSLT 2.0 -- the two actually share
common semantic underpinnings.

But those semantics are a SUPERSET of those of XPath 2.0. The whole can
not easily be reduced to a part.

If you need things that XPath doesn't support by itself, use XQuery or XSLT.
 
D

Dimitre Novatchev

You may need an XQuery interpreter written in XSLT.

I think David Carlisle produced one.


Cheers,
Dimitre Novatchev
 
P

Priscilla Walmsley

Hi,

You could simplify it somewhat, as in:

string-join(
for $r in doc("xtractorTest.xml")//BODY/descendant-or-self::*
return (
$r/@*/concat(upper-case(name($r)),': ', upper-case(name(.)),': ',.)
,
if (normalize-space($r/text()[1]))
then concat(string-join($r/ancestor-or-self::*[position() <
last()]/upper-case(name(.)),': ' ),': ', normalize-space($r/text()[1]))
else ()
),'
')

But Saxon 8 supports running XQuery from Java, so I'm not sure why you
need to convert it to XPath.

If you need to embed it in XSLT for some reason, Saxon also supports
importing an XQuery function library into XSLT (through the
saxon:import-query instruction), so you could just declare your current
query as a function and import it into your XSLT.

Hope that helps,
Priscilla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top