By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

XPath 2.0 expression from XQuery?

P: n/a
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 <XTRACTtags 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

Apr 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
Apr 27 '07 #2

P: n/a
You may need an XQuery interpreter written in XSLT.

I think David Carlisle produced one.
Cheers,
Dimitre Novatchev

"Bloody Viking" <pa************@alum.mit.eduwrote in message
news:11**********************@n15g2000prd.googlegr oups.com...
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 <XTRACTtags 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

Apr 28 '07 #3

P: n/a
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

---------------------------------------------
Priscilla Walmsley
Author, XQuery (O'Reilly Media)
http://www.datypic.com
http://www.xqueryfunctions.com
---------------------------------------------

*** Sent via Developersdex http://www.developersdex.com ***
Apr 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.