需要帮助阻止 MSXML 添加命名空间

发布于 2024-08-18 06:56:52 字数 829 浏览 4 评论 0原文

我正在使用 MSXML 4 生成以下 xml 字符串:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three>
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

但是,我的 IXMLDOMDocument2Ptr 的输出始终包含 Child_Three 的命名空间:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three xmlns="http://1">
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

我的理解是,此行为是 XML 标准的一部分,但如果额外的命名空间,接收 xml 的系统会拒绝它存在。如果存在空命名空间(即 xmlns=""),它也会拒绝 xml。

MSXML 中是否有办法避免添加或删除 Child_Three 的命名空间?

I am using MSXML 4 to generate the following xml string:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three>
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

However the output from my IXMLDOMDocument2Ptr always includes a namespace for Child_Three:

<?xml version="1.0">
<Parent_Element xmlns="http://1">
    <Child_One>
        <Child_Two xmlns="http://2">
            <Child_Three xmlns="http://1">
            </Child_Three>
        </Child_Two>
    </Child_One>
</Parent>

My understanding is that this behavior is part of the XML standard, but the system receiving the xml rejects it if the extra namespace is present. It will also reject the xml if there is an empty namespace (i.e. xmlns="").

Is there anyway in MSXML to avoid adding or removing the namespace for Child_Three?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-08-25 06:56:52

我想通了。

1)我有一个缺陷,使用文档名称空间而不是父节点中的名称空间。

2) 通过 #1 的修复,我最终得到了一个空的命名空间 (xmlns="")。为了纠正这个问题,我必须在创建节点时设置名称空间。在我创建节点然后在单独的调用中添加 xmlns 属性之前。

之前:

pNode->createNode(NODE_ELEMENT, name, "");
pAttrib = pNode->createAttribute("xmlns")
pAttrib->put_NodeValue(namespace)

现在:

pNode->createNode(NODE_ELEMENT, name, "namespace");

I figured it out.

1) I had a defect where the document namespace was used instead of the namespace in the parent node.

2) With the fix from #1, I ended up with an empty namespace (xmlns=""). To corect this I had to set the namepace when the node is created. Before I was creating the node and then added the xmlns attribute in a separate call.

Before:

pNode->createNode(NODE_ELEMENT, name, "");
pAttrib = pNode->createAttribute("xmlns")
pAttrib->put_NodeValue(namespace)

Now:

pNode->createNode(NODE_ELEMENT, name, "namespace");
凯凯我们等你回来 2024-08-25 06:56:52

MSXML 将准确地表示您告诉它表示的名称空间。

从您的引用来看,您似乎创建了名称空间为 http://1 的 child3 节点,并且您需要使用名称空间 < a href="http://2" rel="nofollow noreferrer">http://2。

MSXML will represent exactly the namespaces you tell it to represent.

From your quotation, it looks as if you created the child3 node with a namespace of http://1, and you need to create it with a namespace of http://2.

泛滥成性 2024-08-25 06:56:52

我找到了这个问题的解决方案。问题是 MSXML 无法处理损坏的命名空间...

我最近遇到了一种情况,在最高级别标记中,有一个 xmlns="http://...",但这是错误的。它应该是:xmlns:xsd="http://..."。

一旦我在最上面的 xml 标签中修复了这个问题,我就可以将 xml 标签插入到文档中,而不会到处看到 xmlns="" 。

有趣的是,当您从头开始编写 XML 文档并创建标签层次结构时,您不会获得 xmlns="" 标签。

I found the solution to this problem. The issue is that MSXML cannot handle broken namespaces...

I had a situation recently where in the highest level tag, there was a xmlns="http://...", but this was wrong. It should have been: xmlns:xsd="http://...".

Once i fixed that in the topmost xml tag, i could insert xml tags into documents without seeing the xmlns="" everywhere.

Interestingly when you write a XML document from the start, create the hierarchy of tags, you wont get the xmlns="" tags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文