首页 > 解决方案 > 在 Delphi 中创建 OpenXML app.xml 已添加属性

问题描述

我正在.docx用 Delphi 中的 OpenXML 创建一个文件。当我\docProps\app.xml在 Delphi 中创建生成 docx 时,由于某种原因总是添加一个标签。

我要创建的 XML 文件是这样的:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
    xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  <Template>Normal.dotm</Template>
  <TotalTime>1</TotalTime>
  <Pages>1</Pages>
  <Words>1</Words>
  ...
</Properties>

通过做这个:

var
  Root: IXMLNode;
  Rel: IXMLNode;

  Root := XMLDocument1.addChild('Properties');
  ... //attributes are added here

  Rel := Root.AddChild('Template');
  Rel.NodeValue := 'Normal.dotm';
  Rel := Root.AddChild('TotalTime');
  Rel.NodeValue := '1';
  ...

我期待上面的代码在顶部生成 XML 文件,但我得到了这个:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
  <Template xmlns="">Normal.dotm</Template >
  <TotalTime xmlns=""></TotalTime>
  <Pages xmlns="">1</Pages>
</Properties>

xmlns由于某种原因添加了该属性。有没有办法在顶部实现预期的 XML?

标签: xmldelphi

解决方案


创建元素时显式提供命名空间 URI:

const
  PROP_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties';

var
  Root: IXMLNode;
  Rel: IXMLNode;

  Root := XMLDocument1.AddChild('Properties', PROP_NS);
  Rel := Root.AddChild('Template', PROP_NS);
  Rel.NodeValue := 'Normal.dotm';
  Rel := Root.AddChild('Pages', PROP_NS);
  Rel.NodeValue := '1';

推荐阅读