【问题标题】:Getting nested node with different namespace to root using XPath query使用 XPath 查询获取具有不同命名空间的嵌套节点到根
【发布时间】:2012-10-15 13:23:02
【问题描述】:

我正在努力从 XML 文档中检索节点,该文档与文档的其余部分具有不同的命名空间。

这是 XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.nominet.org.uk/epp/xml/epp-1.0 epp-1.0.xsd">
    <response>
        <result code="1000">
            <msg>Command completed successfully</msg>
        </result>
        <resData>
            <domain:infData xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
                <domain:name>danielormisher.co.uk</domain:name>
                <domain:roid>50798439-UK</domain:roid>
                <domain:registrant>10DBFAD79B81CCDE</domain:registrant>
            </domain:infData>
        </resData>
        <extension>
            <domain-nom-ext:infData xmlns:domain-nom-ext="http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.0" xsi:schemaLocation="http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.0 domain-nom-ext-1.0.xsd">
                <domain-nom-ext:reg-status>Registered until expiry date.</domain-nom-ext:reg-status>
                <domain-nom-ext:auto-bill>30</domain-nom-ext:auto-bill>
            </domain-nom-ext:infData>
        </extension>
    </response>
</epp>

在上面的 XML 中,如果我尝试选择 &lt;domain:infData&gt; 元素,它与根元素共享一个命名空间,如下所示:

namespaces.AddNamespace("domain", "urn:ietf:params:xml:ns:domain-1.0");
var children = doc.SelectSingleNode("/ns:epp/ns:response/ns:resData/domain:infData", namespaces);

节点选择正确。但是,如果我尝试选择节点 &lt;domain-nom-ext:infData&gt;,它与根目录具有不同的命名空间,如下所示:

namespaces.AddNamespace("domain", "urn:ietf:params:xml:ns:domain-1.0");
namespaces.AddNamespace("domain-nom-ext", "http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.1");
children = doc.SelectSingleNode("/ns:epp/ns:response/ns:extension/domain-nom-ext:infData", namespaces);

我只是得到一个空响应。谁能指出我做错了什么??

【问题讨论】:

  • 好像打错了:http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.1 应该是http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.0

标签: c# .net xml xpath


【解决方案1】:

您的 XML 示例有 http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.0,您的代码使用稍微不同的命名空间 http://www.nominet.org.uk/epp/xml/domain-nom-ext-1.1(即 1.1 而不是 1.0。这足以使您的代码不选择任何元素,因为您需要使用完全相同的元素命名空间名称。

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多