473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XPath 2.0 expression from XQuery?

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
3 3530
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: inquirydog | last post by:
Can anyone explain to me why the following XQuery expression (a simple xpath expression) returns a different result than the same expression in xslt? document("document.xml")//a/@b For the...
8
by: Terry P | last post by:
Are there any tools (java classes, tag libraries) which can translate xpath statements into a SQL query? Given an xpath query which has a predicate that filters node values or attributes, I want...
1
by: Philipp Schumann | last post by:
Hi .NET XML fans, does anyone know ad hoc whether support for the above standards is planned for .NET 2.0? I suppose this would be extremely valuable for many folks... Thanks, Phil
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
1
by: Oleg Paraschenko | last post by:
Hello, those who are still interested in "lisp vs xml" and "xml is a poor copy of s-expressions" issues, might find my recent writings interesting: Towards s-expression based XPath/XSLT...
1
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
2
by: arunairs | last post by:
Hi, Is there a way of validating and XPath? In this case , I am accepting an XPath from the user and I need to validate the XPath syntax. Is there a regular expression available that anyone can...
3
by: Arndt Jonasson | last post by:
Let's say we have a schema (maybe expressed in XML Schema, but not necessarily so), that allows this instance document: <top> <txt>This is text</txt> <books> <book>Tarzan</book> <book>Harry...
4
by: ducttape | last post by:
Hi, I have been trying for several days to read XML files into my program. I have been following several good tutorials on the internet, but I am struggling because the particular XML files that I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.