3

给定以下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>

我无法使用 BizTalks XPathMutatorStream 类查询 hl7:MCCI_IN000002UV01/hl7:versionCode/@code 属性,除非我更改了 xml 并删除了命名空间前缀。例如,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>

不幸的是,我无法更改 xml,所以我必须处理 ns0 前缀。

基本上,我通过传递一个流来创建一个 XMLReader 对象:

XmlReader xr = XmlReader.Create(strMyStream);

然后我用 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"));

我将 XPathCollection 和 XmlReader 实例传递给 BizTalk XPathMutatorStream 对象:

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

如果 xml 上没有命名空间前缀,这一切都可以正常工作,但只要有,我就永远不会得到任何匹配。我需要在命名空间管理器上或在实际的 xpath 语句中做些什么来获得匹配吗?

4

2 回答 2

2

您是否尝试使用 local-name() 函数?

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

于 2014-10-17T22:17:04.423 回答
0

如果你想使用 Linq2Xml:

var xDoc = XDocument.Load(filename);

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-使用 Linq

XNamespace h17 = "urn:hl7-org:v3";
var code2 = xDoc.Descendants(h17 + "versionCode").First().Attribute("code").Value;
于 2014-10-17T20:45:12.127 回答