Question

It is driving me crazy.

Can someone tell me why this query does not work:

xquery version "3.0";
for $item in collection("openkernel/openehr_ehr/archetyped/")
let $uid:=$item//uid/value
where $uid="51160740-171e-487c-a04d-eae267f7079a"
return $item

Must be something stupid, I know. The double slash before //uid/value is because I want to use the query generic

The XML-document I am trying to find resides in this collection, and is this:

<openehr-ehr_rm-Composition.composition.v1 xmlns="http://rosa.openkernel/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://rosa.openkernel/ file:/openehr-ehr_rm-Composition.composition.v1.xsd">
<Composition archetype_id="openehr-ehr_rm-Composition.composition.v1">
    <archetype_id>
        <value>openehr-ehr_rm-Composition.composition.v1</value>
    </archetype_id>
    <category>
        <defining_code>
            <code_string>431</code_string>
            <terminology_id>
                <value>openehr</value>
            </terminology_id>
        </defining_code>
        <value>persistent</value>
    </category>
    <something>a composition</something>
    <uid>
        <value>0e15d0f2-0b59-4df7-88f8-27be87e1e2ac</value>
    </uid>
    <content archetype_id="openehr-ehr_rm-ADMIN_ENTRY.admin_entry.v1" archetype_node_id="at0002">
        <archetype_id>
            <value>openehr-ehr_rm-ADMIN_ENTRY.admin_entry.v1</value>
        </archetype_id>
        <an_item>nono</an_item>
        <an_other_item>an_other_item</an_other_item>
        <something>an admin_entry</something>
        <uid>
            <value>51160740-171e-487c-a04d-eae267f7079a</value>
        </uid>
    </content>
</Composition>
</openehr-ehr_rm-Composition.composition.v1>

Thanks, very much Bert

Was it helpful?

Solution

You need to take the namespace into account, see http://www.w3.org/TR/xquery/#id-default-namespace:

declare default element namespace "http://rosa.openkernel/";

OTHER TIPS

This is an namespace issue.

xquery version "3.0";
declare namespace rosa = "http://rosa.openkernel/";

for $item in collection("openkernel/openehr_ehr/archetyped/")
let $uid:=$item//rosa:uid/rosa:value
where $uid="51160740-171e-487c-a04d-eae267f7079a"
return $item

Alternatively, you could declare the default namespace using

declare default element namespace "http://rosa.openkernel/";

and use the code you had so far. Or you select the <uid/> and <value/> elements of all namespaces using a wildcard for it:

let $uid:=$item//*:uid/*:value

You can try like bellow, its written in C#

  1. declaring the XmlNamespaceManager

XmlDocument doc = new XmlDocument(); var namespaceManager = new XmlNamespaceManager(doc.NameTable);

  1. Load the doc using XML reader or any other way

doc.Load(reader); namespaceManager.AddNamespace("a", doc.DocumentElement.NamespaceURI);

  1. Query the desired data as bellow
HierObjectId uid = null;
XmlNode uidNode = doc.SelectSingleNode("//a:archetype/a:uid/a:value", namespaceManager);
if (uidNode == null)
{
     uid = HierObjectId.NewObjectId();
}
else
{
  uid = new HierObjectId(uidNode.InnerText);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top