2

I need to fetch the <TITLE> element from the following XML using XQuery (I'm using eXist-db).

<?xml-stylesheet href="shakes.xsl" type="text/xsl"?>
<!--!DOCTYPE PLAY PUBLIC "-//PLAY//EN" "play.dtd"-->
<ClinicalDocument xmlns="urn:hl7-org:v3">
    <PLAY>
        <TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
    </PLAY>
</ClinicalDocument >

When I try with the XQuery below I am not getting the expected output.

xquery version "3.0";

doc("/db/apps/demo/data/hamlet.xml")/ClinicalDocument/PLAY/TITLE

I think the problem is related to the xmlns attribute present in the <ClinicalDocument> tag:

<ClinicalDocument xmlns="urn:hl7-org:v3">

How can I modify my XQuery to retrieve the desired XML element?

Joe Wicentowski
  • 5,159
  • 16
  • 26
Arun M R Nair
  • 653
  • 7
  • 30

2 Answers2

1

Declare a prefix that mapped to your XML default namespace, and use that prefix to reference elements in the namespace, something like this :

declare namespace d = "urn:hl7-org:v3";
doc("/db/apps/demo/data/hamlet.xml")/d:ClinicalDocument/d:PLAY/d:TITLE

tested here : http://www.xpathtester.com/xquery/a88f300bbeacdbd846d7aec887d48a0b

har07
  • 88,338
  • 12
  • 84
  • 137
1

For any namespace, you can use the * wildcard in place of the namespace prefix.

So if you just want the text of the title, you can do:

doc("/db/apps/demo/data/hamlet.xml")/*:ClinicalDocument/*:PLAY/*:TITLE/text()

There is no longer the need to define the namespace. (Although you'll find that having a defined schema with a namespace makes working with XQuery much nicer in the long run.)

westbaystars
  • 151
  • 1
  • 4