How do I add a namespace to an msxml DOMDocument?

Viewed 3218

How do I add a schema to an IXMLDOMDocument?

For example, I want to generate the XML:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Id="rId1" Type="Frob" Target="Grob"/>
</Relationships>

I can construct the DOMDocument60 object (pseudo-code):

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");

Now comes the question of how to add the namespace.

How to add the namespace?

If I do the obvious solution, setting an attribute on the Relationships node called xmlns:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">

through something like:

relationships.setAttribute("xmlns", 
      "http://schemas.openxmlformats.org/package/2006/relationships");

When the document is saved, it causes the resulting xml to be wrong:

<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
   <Relationship Id="rId1" Type="Frob" Target="Grob" xmlns=""/>
</Relationships>

It places empty xmlns attributes on every other element. In this little test document it only misapplies the xmlns to one element. In the real world there are dozens, or a few million other elements with an empty xmlns attribute.

namespaceURI property

I tried setting the namespaceURI property of the Relationships element:

relationshps.namespaceURI := "http://schemas.openxmlformats.org/package/2006/relationships"; 

but the property is read-only.

schemas Property

The document does have a schemas property, which gets or sets an XMLSchemaCache object. But it requires an actual schema document. E.g. trying to just set a schema doesn't work:

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

But that tries to actually load the schema url, rather than not loading the schema because it isn't a URI.

Perhaps I have to randomly try other things:

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

But that causes no xmlns to be emitted.

Rather than trying to build an XML document the correct way, I could always use a StringBuilder to build the XML manually, and then have parse it into an XML Document object.

But I'd rather do it the right way.

1 Answers
Related