0

I work as Information Developer.

I need to create .dita files with the file names stored in an excel sheet.

The .dita files are XML files. The structure of the files should be same. The structure is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!--Arbortext, Inc., 1988-2011, v.4002-->
<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN"
"concept.dtd">
<?Pub Inc?>
<concept id="concept-1-43B80068" xml:lang="en">
<title></title><?Pub Caret -1?>
<shortdesc></shortdesc>
<conbody>
<p></p>
</conbody>
</concept>

and then I need to create a map of these files as follows:

<map xml:lang="en">
<topicref href = "topic1.dita">
<topicref href = "topic2.dita">
</topicref>
</map>

Please provide a solution to this.

laurent
  • 88,262
  • 77
  • 290
  • 428

3 Answers3

2

Assuming you know how to program in Python and run Python apps, you can try the following where you put your file names in a list. If you need to read from Excel, then you can use one of the packages for reading Excel files in Python and use that in place of filelist below.

import os

filelist = ["topic1",
"topic2"
]    # array of dita files

basedir = r'C:/DITA_files/'

# create files
if not os.path.exists(basedir):
    os.mkdir(basedir) 

conceptstr = '''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN"
"concept.dtd">
<?Pub Inc?>
<concept id="concept-1-%s" xml:lang="en">
<title></title><?Pub Caret -1?>
<shortdesc></shortdesc>
<conbody>
<p></p>
</conbody>
</concept>
'''

id_len = 8
# create concept files and map
for file in filelist:
    print 'Writing ' + basedir + file + '.dita'
    cf = open(basedir + file +'.dita', 'w')
    cf.write(conceptstr) % [random.choice(string.ascii_letters) for n in xrange(id_len)]
    cf.close()

map = open(basedir + 'mymap.ditamap','wt')

map.write('<map xml:lang="en">')
for file in filelist:
    map.write(r'''<topicref href="%s.dita"/>
''' % ( register, register ) )
    map.write('</map>')
    map.close()
Bellave Jayaram
  • 366
  • 1
  • 2
  • 11
2

You also can create a VBA macro for it in Excel. Select the cell with the filename in it and run the macro. Attention: To get double quotes in the output you need to put them twice as ""

Sub CreateDitaConcept()
Dim sFileName As String
Dim iFileNum As Integer

'Read filename from selected cell
sFileName = Selection.Value
'Get a free file number
iFileNum = FreeFile
'Open the file for output
Open sFileName For Output As iFileNum
'Print text to the file
Print #iFileNum, "<?xml version=""1.0"" encoding=""UTF-8""?>"
Print #iFileNum, "<!-- Arbortext etc -->"
Print #iFileNum, "<!DOCTYPE concept PUBLIC ""-//OASIS//DTD DITA Concept//EN"" ""concept.dtd"">"
Print #iFileNum, "<concept id=""concept_id"" xml:lang=""en"">"
Print #iFileNum, "<title>Concept title</title>"
Print #iFileNum, "<shortdesc></shortdesc>"
Print #iFileNum, "<conbody>"
Print #iFileNum, "<p>paragraph</p>"
Print #iFileNum, "</conbody>"
Print #iFileNum, "</concept>"
'Close file
Close #iFileNum

End Sub

Ben Welman
  • 301
  • 2
  • 4
1

Do note that your DITA markup is invalid. All DITA topics must have a <title> element that contains content. You might want to add some placeholder text as content of the <title> element, for example:

<title>Title goes here</title>

Also, you do not need the below markup, as these are processing instructions added by PTC's Arbortext Editor:

<?Pub Inc?>
<?Pub Caret -1?>
Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81