如何将命名空间添加到 msxml DOMDocument?

Ian*_*oyd 4 winapi msxml ixmldomdocument

如何将架构添加到IXMLDOMDocument?

例如,我想生成 XML:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Id="rId1" Type="Frob" Target="Grob"/>
</Relationships>
Run Code Online (Sandbox Code Playgroud)

我可以构造 DOMDocument60 对象(伪代码)

DOMDocument60 doc = new DOMDocument60();

IXMLDOMElement relationships = doc.appendChild(doc.createElement("Relationships"));

IXMLDOMElement relationship = relationships.appendChild(doc.createElement("Relationship"));
   relationship.setAttribute("Id", "rId1");
   relationship.setAttribute("Type", "Frob");
   relationship.setAttribute("Target", "Grob");
Run Code Online (Sandbox Code Playgroud)

现在是如何添加命名空间的问题。

如何添加命名空间?

如果我执行明显的解决方案,请在关系节点上设置一个名为xmlns

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
Run Code Online (Sandbox Code Playgroud)

通过类似的东西:

relationships.setAttribute("xmlns", 
      "http://schemas.openxmlformats.org/package/2006/relationships");
Run Code Online (Sandbox Code Playgroud)

文档保存时,导致生成的xml出错:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Id="rId1" Type="Frob" Target="Grob" xmlns=""/>
</Relationships>
Run Code Online (Sandbox Code Playgroud)

xmlns在每个其他元素上放置空属性。在这个小测试文档中,它只会错误地应用xmlns到一个元素。在现实世界中,有数十个或数百万个具有空xmlns属性的其他元素。

命名空间URI属性

我尝试设置元素的namespaceURI属性Relationships

relationshps.namespaceURI := "http://schemas.openxmlformats.org/package/2006/relationships"; 
Run Code Online (Sandbox Code Playgroud)

但该属性是只读的

模式属性

文档确实有一个schemas属性,它获取或设置一个XMLSchemaCache对象。但它需要一个实际的模式文档。例如,尝试仅设置架构不起作用:

schemas = new XMLSchemaCache60();
schemas.add('', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
doc.schemas := schemas;
Run Code Online (Sandbox Code Playgroud)

但这会尝试实际加载架构 url,而不是不加载架构,因为它不是 URI。

也许我必须随机尝试其他事情:

schemas = new XMLSchemaCache60();
schemas.add('http://schemas.openxmlformats.org/spreadsheetml/2006/main', null);
doc.schemas := schemas;
Run Code Online (Sandbox Code Playgroud)

但这会导致 noxmlns被发射。

所以现在我放弃并询问 Stackoverflow。

与其尝试以正确的方式构建 XML 文档,我始终可以使用 aStringBuilder手动构建 XML,然后将其解析为 XML 文档对象。

但我宁愿以正确的方式去做。

Ian*_*oyd 5

诀窍是实现 W3C DOM Level 2 和 3 有一个方法 createElementNS

创建具有指定命名空间 URI 和限定名称的元素。

句法

element = document.createElementNS(namespaceURI, qualifiedName);

但是 MSXML 6 只支持 DOM Level 1。

幸运的是,W3C DOM Level 1 确实有一个方法来创建一个带有命名空间的元素: createNode

使用提供的类型、名称和命名空间创建节点。

HRESULT createNode(VARIANT Type, BSTR name, BSTR namespaceURI, out IXMLDOMNode node);
Run Code Online (Sandbox Code Playgroud)

因此我的解决方案是我必须改变:

relationships: IXMLDOMElement = doc.createElement("Relationships"); 
Run Code Online (Sandbox Code Playgroud)

进入:

const NODE_ELEMENT: Integer = 1;
const ns: string = "http://schemas.openxmlformats.org/package/2006/relationships";

relationships: IXMLDOMElement = doc.createNode(NODE_ELEMENT, "Relationships", namespace); 
Run Code Online (Sandbox Code Playgroud)

一个糟糕的部分是每个元素都必须在该命名空间中创建:

function AddElementNS(IXMLDOMNode parentNode, String tagName, String namespaceURI): IXMLDOMElement;
{
   doc: IXMLDOMDocument = parentNode as IXMLDOMDocument;
   if (doc == null) 
      doc = parentNode.ownerDocument;

   if (namespaceURI <> "")
      Result = doc.createNode(NODE_ELEMENT, tagName, namespaceURI)
   else
      Result = doc.createElement(tagName);

   parentNode.appendChild(Result);
}

relationships: IXMLDOMElement = AddElementNS(doc, "Relationships", ns);

relationship: IXMLDOMElement = AddElementNS(relationships, "Relationship", ns);
   relationship.setAttribute("Id", "rId1");
   relationship.setAttribute("Type", "Frob");
   relationship.setAttribute("Target", "Grob");       
Run Code Online (Sandbox Code Playgroud)

奖励阅读