Home > Enterprise >  Creating OpenXML app.xml in Delphi has added attributes
Creating OpenXML app.xml in Delphi has added attributes

Time:09-17

I'm working on creating a .docx file with OpenXML in Delphi. When I created \docProps\app.xml in Delphi to generate the docx, a tag is always added for some reason.

The XML file I want to create is this:

<?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>

By doing this:

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';
  ...

I was expecting the above code to generate the XML file at the top, but I got this:

<?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>

The xmlns attribute is added for some reason. Is there a way to achieve the expected XML at the top?

CodePudding user response:

Explicitly provide the namespace URI when you create elements:

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';
  • Related