如何使用TXMLDocument向每个节点添加名称空间前缀 [英] How do I add a namespace prefix to each node using TXMLDocument

查看:189
本文介绍了如何使用TXMLDocument向每个节点添加名称空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XML绑定向导创建了TXMLDocument的后代。此类生成的文件将在根节点中声明名称空间,并为文档的其余部分创建普通的,未经修饰的节点。

I used the XML Binding Wizard to create a descendant of TXMLDocument. The files generated by this class would declare the namespace in the root node and create just plain, unadorned nodes for the rest of the document.

<?xml version="1.0"?>
<RootNode xmlns="URL" xmlns:xsi="URL" xsi:schemaLocation="URL">
    <SomeNode>
        <AnotherNode>Value</AnotherNode>
    </SomeNode>
</RootNode>

我完全可以轻松阅读或验证此内容。但是,现在发送这些文件的处理器要求每个节点都具有命名空间前缀,以便正确处理文件。

I've had no trouble reading or validating this at all. However, the processor where these files are sent now requires each node to have the namespace prefixed in order to process files correctly.

<?xml version="1.0"?>
<NS:RootNode xmlns:NS="URL" xmlns:xsi="URL" xsi:schemaLocation="URL">
    <NS:SomeNode>
        <NS:AnotherNode>Value</NS:AnotherNode>
    </NS:SomeNode>
</NS:RootNode>

如何使用TXMLDocument后代来完成此操作?我希望它不涉及手工编辑10000行生成的代码。

How do I accomplish this with my TXMLDocument descendant? I hope it doesn't involve hand editing 10000 lines of generated code.

推荐答案

好,解决方案花了很长时间才发现

Ok, the solution took a long time to discover but was surprisingly simple.

XML数据绑定向导生成的代码将使用默认名称空间创建xml。您可以通过检查 Get Load New 在生成的单元中起作用。这三个都调用 GetDocBinding ,并传入 TargetNamespace 作为最终参数。 TargetNamespace 是一个全局常量字符串,其中的URI是从您提供给绑定向导的架构或xml文档中提取的。

The code generated by XML Data Binding Wizard will create xml using the default namespace. You can see this by examining the Get, Load and New functions in the generated unit. All three make calls to GetDocBinding, passing in TargetNamespace as the final parameter. TargetNamespace is a global constant string with the URI extracted from the schema or xml document you fed to the Binding Wizard.

因为 TargetNamespace 被分配给根元素作为默认名称空间,所以子元素都没有前缀。

Because TargetNamespace is assigned to the root element as the default namespace no child elements will have a prefix.

执行此操作的方式:

FDocumentName := 
  NewXMLDocument.GetDocBinding(
    'ns:DocumentName', // <-- Just add the prefix to the root node.
    TXMLDocumentName,
    TargetNamespace) as IXMLDocumentName;

现在,根节点看起来像:

Now the root node will look like:

<ns:DocumentName xmlns:ns="URI">

所有子节点在创建时都将具有前缀。

And all child nodes will have the prefix when they are created.

这篇关于如何使用TXMLDocument向每个节点添加名称空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆