Skip to content

Auto-insert data into a DITA XML file

We want to automate the generation of the following DITA file:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA 1.2 Topic//EN"
"/usr/share/dita-ot/dtd/technicalContent/dtd/topic.dtd">
<topic id="products-and-versions">
<title>Products and versions</title>
<body>
<p>Dianthus</p>
<ul>
<li>1.0</li>
<li>1.5</li>
<li>2.3</li>
</ul>
<p>Geum</p>
<ul>
<li>1.0</li>
<li>1.5</li>
<li>2.3</li>
</ul>
<p>Prunus</p>
<ul>
<li>1.0</li>
<li>1.5</li>
<li>2.3</li>
</ul>
</body>
</topic>
  1. Install the following programs and libraries:

    Terminal window
    $ sudo apt install libxml2-dev libxslt1-dev python3-lxml
  2. Create the following Python script populate-xml.py:

    code/populate-xml.py
  3. Make the script executable, then run it:

    Terminal window
    $ chmod +x populate-xml.py
    $ ./populate-xml.py

    The template.dita file is created and contains the desired data.

This approach minimizes the risk of errors and reduces the effort involved in updating.