简体   繁体   English

带有名称空间前缀的xml的XPath表达式

[英]XPath expression for xml with a namespace prefix

Given the following xml: 给出以下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. 我无法使用BizTalks XPathMutatorStream类查询hl7:MCCI_IN000002UV01 / hl7:versionCode / @ code属性,除非我更改了xml并删除了名称空间前缀。 So for example, the xml would now look like this: 例如,xml现在看起来像这样:

<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. 不幸的是我无法更改xml,所以我必须处理ns0前缀。

Basically, I create an XMLReader object by passing it a stream: 基本上,我通过传递一个流创建一个XMLReader对象:

XmlReader xr = XmlReader.Create(strMyStream);

Then I create my XPathCollection with an XPathExpression: 然后我用XPathExpression创建我的XPathCollection:

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: 我将我的XPathCollection和XmlReader实例传递给BizTalk XPathMutatorStream对象:

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. 如果xml上没有名称空间前缀,这一切都正常,但只要有,我就永远不会得到任何匹配。 Is there something I need to do on the namespace manager, or in the actual xpath statements to get a match? 我需要在命名空间管理器或实际的xpath语句中做些什么来获得匹配吗?

Did you try using the local-name() function? 您是否尝试使用local-name()函数?

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

If you want to use Linq2Xml: 如果你想使用Linq2Xml:

var xDoc = XDocument.Load(filename);

1- Using XPath 1-使用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 2-使用Linq

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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