user461051
user461051

Reputation: 431

How do you refer to an xml node that has two namespace definitions?

I have an xml message from a 3rd party that has a node:

<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:npfitlc="NPFIT:HL7:Localisation" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
classCode="DOCCLIN" moodCode="EVN">

I created a Namespace object to use to identify npfitlc items under this node:

ns.AddNamespace("npfitlc", "NPFIT:HL7:Localisation");

But when I try to choose the ClinicalDocument node it can't find it:

XmlNode myNode = soapEnvelop.SelectSingleNode
        ("//soap:Envelope/soap:Body/itk:DistributionEnvelope/itk:payloads/itk:
        payload/ClinicalDocument", ns);

As you can see in my doc there are multiple nodes to get to Clinical Document. And when I reference down to itk:payload it locates it fine:

XmlNode myNode = soapEnvelop.SelectSingleNode
       ("//soap:Envelope/soap:Body/itk:DistributionEnvelope/itk:
       payloads/itk:payload", ns);

I took out xmlns="urn:hl7-org:v3" from the ClinicalDocument tag and then I could find it find with my SelectSingleNode call, but the system I sent the message to fails validation because that is missing.

I am not sure how to handle it where there is a "root" namespace defined in that node.

Upvotes: 0

Views: 207

Answers (1)

JLRishe
JLRishe

Reputation: 101738

ClinicalDocument has no prefix and it has an xmlns="urn:hl7-org:v3" namespace declaration, which means that its namespace is urn:hl7-org:v3. The rest of the namespace declarations there are completely irrelevant for the purpose of selecting this particular element.

So what you need to do is...

Add that namespace to your namespace manager (using any nonempty prefix):

ns.AddNamespace("hl", "urn:hl7-org:v3");

Use that prefix in your XPath:

XmlNode myNode = 
    soapEnvelop.SelectSingleNode("//soap:Envelope/soap:Body" + 
                                  "/itk:DistributionEnvelope/itk:payloads" + 
                                  "/itk:payload/hl:ClinicalDocument", ns);

and that should do it.

Upvotes: 3

Related Questions