にスキーマを追加するにはどうすればよいIXMLDOMDocument
ですか?
たとえば、次の XML を生成したいとします。
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="Frob" Target="Grob"/>
</Relationships>
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");
ここで、名前空間を追加する方法が問題になります。
名前空間を追加するには?
明らかな解決策を実行すると、 Relationshipsノードに次の属性を設定しますxmlns
。
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
次のようなものを通して:
relationships.setAttribute("xmlns",
"http://schemas.openxmlformats.org/package/2006/relationships");
ドキュメントを保存すると、結果の xml が間違ったものになります。
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="Frob" Target="Grob" xmlns=""/>
</Relationships>
他のすべての要素に空のxmlns
属性を配置します。この小さなテスト ドキュメントでは、xmlns
を 1 つの要素に誤って適用するだけです。xmlns
現実の世界には、空の属性を持つ他の要素が数十、または数百万あります。
namespaceURI プロパティ
namespaceURI
要素のプロパティを設定してみましたRelationships
:
relationshps.namespaceURI := "http://schemas.openxmlformats.org/package/2006/relationships";
ただし、プロパティは読み取り専用です。
schemas プロパティ
ドキュメントには、オブジェクトschemas
を取得または設定するプロパティがありXMLSchemaCache
ます。ただし、実際のスキーマ ドキュメントが必要です。たとえば、スキーマを設定しようとしてもうまくいきません:
schemas = new XMLSchemaCache60();
schemas.add('', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
doc.schemas := schemas;
ただし、URI ではないためにスキーマをロードしないのではなく、実際にスキーマ URL をロードしようとします。
おそらく、他のことをランダムに試す必要があります。
schemas = new XMLSchemaCache60();
schemas.add('http://schemas.openxmlformats.org/spreadsheetml/2006/main', null);
doc.schemas := schemas;
しかし、それにより noxmlns
が発行されます。
XML ドキュメントを正しい方法で構築しようとするのではなく、いつでも を使用しStringBuilder
て手動で XML を構築し、それを解析して XML ドキュメント オブジェクトにすることができます。
しかし、私はむしろそれを正しい方法で行いたいと思います。