简体   繁体   中英

XPath expression for xml with a 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/@code attribute using BizTalks XPathMutatorStream class unless I altered the xml and removed the namespace prefix. So for example, the xml would 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 can't change the xml, so I have to deal with the ns0 prefix.

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

XmlReader xr = XmlReader.Create(strMyStream);

Then I create my XPathCollection with an 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"));

The I pass my XPathCollection and XmlReader instances to a BizTalk XPathMutatorStream object:

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

This all works fine if there is no namespace prefix on the xml but as soon as there is, I never get any matches. Is there something I need to do on the namespace manager, or in the actual xpath statements to get a match?

Did you try using the local-name() function?

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM