9

I'm using eXist-DB to store XML documents. Here's a sample XML file with the namespace info:

<?xml version="1.0" encoding="utf-8"?>
<ClinicalDocument xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                  xmlns="urn:hl7-org:v3">
            <name>
                <family>Smith</family>
                <middle>Blade</middle>
                <first>John</first>
            </name>
            <name>
                <family>Frost</family>
                <middle>Bill</middle>
                <first>Conrad</first>
            </name>
</ClinicalDocument>

The query is :

declare namespace xsd = "http://www.w3.org/2001/XMLSchema";
declare namespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

for $x in doc("/db/HL7/cda2.xml")/ClinicalDocument/name
return $x/family

I tried to declare the third namespace within the query but it doesn't seem to work.

jwesonga
  • 4,249
  • 16
  • 56
  • 83
  • One of many duplicates: [How does one bind namespace prefixes when using QXmlQuery (Qt XQuery)?](http://stackoverflow.com/questions/5011557/how-does-one-bind-namespace-prefixes-when-using-qxmlquery-qt-xquery) –  Mar 10 '11 at 17:24

1 Answers1

11

The third namespace attribute (xmlns="urn:hl7-org:v3") binds the default namespace.

The syntax to define the default namespace for elements in XQuery is:

declare default element namespace "urn:hl7-org:v3";

However, redefining the default element namespace prevents your query from testing for nodes with no namespace!

A better way to do this is perhaps to bind a new prefix to this namespace:

declare namespace xsd = "http://www.w3.org/2001/XMLSchema";
declare namespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
declare namespace h17 = "urn:h17-org:v3";

for $x in doc("/db/HL7/cda2.xml")/h17:ClinicalDocument/h17:name
return $x/h17:family
Oliver Hallam
  • 4,242
  • 1
  • 24
  • 30