Page 1 of 1

Can't make XQuery do anything

Posted: Thu Oct 03, 2013 1:50 am
by wolandscat
[Windows 7 / Oxygen Editor v15]

I have a very simple Xquery:

for $x in doc("../openHR_test_set_001/039dec8d-7b2b-485d-be37-ff5e29645e71.xml")/openHealthRecord
return $x/id

The start of the data looks as follows:

<?xml version="1.0"?>
<openHealthRecord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" schemaVersion="1.3.0" xmlns="http://www.e-mis.com/emisopen">
<id>028d34e2-250d-4c5e-aa35-10d0eb8b257b</id>
<creationTime>2010-05-09T00:00:00+01:00</creationTime>


I have set up a transformation scenario to run this query; it validates and runs, but the parser (Saxon-PE XQuery 9.5.0.2) says: Your query returned an empty sequence.

The doc() function is working and if I run it in debug mode it finds the xml source doc.

I assume I am just not configuring something somewhere, but I have run out of things to try...

thanks for any help.

- thomas beale

Re: Can't make XQuery do anything

Posted: Thu Oct 03, 2013 11:53 am
by adrian
Hi,

You forgot about the default namespace of the elements from the XML file (xmlns="http://www.e-mis.com/emisopen").

There are at least three ways you can handle this (choose the one that best fits your needs):
1. If you do want to refer the elements by name and namespace, declare and use a namespace prefix:

Code: Select all

declare namespace emis = "http://www.e-mis.com/emisopen";
for $x in doc("input.xml")/emis:openHealthRecord
return <a>{$x/emis:id}</a>
2. If you don't care about the specific namespace, and only want to specify the elements by local name use the "any"(*) namespace prefix:

Code: Select all

for $x in doc("input.xml")/*:openHealthRecord
return $x/*:id
3. If all the elements you work with (including output) are from the same namespace, you can simply declare the default element namespace at the top of the XQuery and use the rest as it is:

Code: Select all

declare default element namespace "http://www.e-mis.com/emisopen";
for $x in doc("input.xml")/openHealthRecord
return $x/id
2. and 3. are the easy way out of this, 1. would be the correct way to handle this.

Regards,
Adrian

Re: Can't make XQuery do anything

Posted: Thu Oct 03, 2013 9:40 pm
by wolandscat
I finally figured this out after doing a textbook example out of desperation. I don't usually work directly in XML, and it always mystifies me when a namespace that is declared in a document isn't the assumed namespace for references (like paths) in a query or other doc, which is processing that doc.

But... those are the rules, and your alternate ways of doing it are very useful, so thanks for taking the time to respond.

- thomas