XPath expression for xml with namespace prefix

Given the following xml:

<ns0:MCCI_IN000002UV01 xmlns:ns0="urn:hl7-org:v3">
    <ns0:id root="2.16.840.1.113883.3.277.100.1" extension="68423f2b-397a-4de4-8b8d-ea1f6c174954" />
    <ns0:creationTime>201410171106-0700</ns0:creationTime>
    <ns0:versionCode code="Ballot2009May" />
    <ns0:interactionId root="2.16.840.1.113883.1.6" extension="MCCI_IN000002UV01" />
    <ns0:processingCode code="P" />
    <ns0:processingModeCode code="T" />
    <ns0:receiver nullFlavor="NA">
        <ns0:device nullFlavor="NA" classCode="DEV" determinerCode="INSTANCE">
            <ns0:id nullFlavor="NA" />
        </ns0:device>
    </ns0:receiver>
    <ns0:sender nullFlavor="NA">
        <ns0:device nullFlavor="NA" classCode="DEV" determinerCode="INSTANCE">
            <ns0:id nullFlavor="NA" />
        </ns0:device>
    </ns0:sender>
    <ns0:acknowledgement typeCode="CA">
        <ns0:targetMessage>
            <ns0:id root="2.16.840.1.113883.3.277.100.1" extension="adb32b05-bf62-4417-8c62-d37a65380c4f" />
        </ns0:targetMessage>
        <ns0:acknowledgementDetail typeCode="I" />
    </ns0:acknowledgement>
</ns0:MCCI_IN000002UV01>

      

I was unable to query the hl7: MCCI_IN000002UV01 / hl7: versionCode / @ attribute using the BizTalks XPathMutatorStream class unless I changed the xml and removed the namespace prefix. So, for example, the xml will now look like this:

<MCCI_IN000002UV01 xmlns="urn:hl7-org:v3">
    <id root="2.16.840.1.113883.3.277.100.1" extension="68423f2b-397a-4de4-8b8d-ea1f6c174954" />
        ...
</MCCI_IN000002UV01>

      

Unfortunately, I cannot change the xml, so I have to deal with the ns0 prefix.

Basically, I create an XMLReader object by passing a stream to it:

XmlReader xr = XmlReader.Create(strMyStream);

      

Then I create my XPathCollection using XPathExpression:

XPathCollection xc = new XPathCollection();
xc.NamespaceManager = new XmlNamespaceManager(xr.NameTable);
xc.NamespaceManager.AddNamespace("hl7", "urn:hl7-org:v3");
xc.Add(new XPathExpression("hl7:MCCI_IN000002UV01/hl7:versionCode/@code"));

      

I am passing my XPathCollection and XmlReader instances to the BizTalk XPathMutatorStream object:

XPathMutatorStream str = new XPathMutatorStream(xr, xc, ...);

      

This all works fine if there is no namespace prefix in the xml, but once that happens I never get any matches. Is there something I need to do in the namespace manager or valid xpath statements to get the match?

+3


source to share


2 answers


Have you tried using the local-name () function?



For example: // * [local-name () = 'MCCI_IN000002UV01'] / * [local-name () = 'versionCode'] / @ code

+2


source


If you want to use Linq2Xml:

var xDoc = XDocument.Load(filename);

      

1 - Using XPath



XmlNamespaceManager mgr = new XmlNamespaceManager(xDoc.CreateNavigator().NameTable);
mgr.AddNamespace("hl7", "urn:hl7-org:v3");

var version = xDoc.XPathSelectElement("hl7:MCCI_IN000002UV01/hl7:versionCode", mgr);
var code = version.Attribute("code").Value;

      

2 - Using Linq

XNamespace h17 = "urn:hl7-org:v3";
var code2 = xDoc.Descendants(h17 + "versionCode").First().Attribute("code").Value;

      

0


source







All Articles