Hi Piotr, and others,
The previous 3 postings have been about how to create a DP XML file, and
how
to use it with Word 2003 for an XML "mail merge" type application.
In the first part, we used Word to open up the DPNews.doc file, and how
to
apply a transformation within Word to generate the new document. The
second
part showed how to convert the structure of theXML file to a Schema
which
could be used by Word to create a templae document, the third posting
showed
how to creata a Template dcument with Word.
Once you have created a template document, it is not essential to use
Word
to create new documents based on this template. It is possible to use
other
programs (some free) to generate the merged Word 2003 and document, into
an
ordinary documents that an everyday Word users could open without having
any
understanding of how it was created. To them it is a native Word
document.
There are many programs that can perform XSLT transformations. Word of
course was one of them, but also you can use many applications, including
say the free WMHelp XMLPad I used to create the DTD and Schema files in
my
second posting. There are many other such as XMLSpy, Oxygen, but my
favourite is an open source commandline utility xsltproc.exe. It is
available for both the Linux/Unix or Windows operating systems. You can
find
details and documentation for it at
http://xmlsoft.org/XSLT/xsltproc2.html
and download the Windows version from
http://www.zlatkovic.com/pub/libxml/libxslt-1.1.23+.win32.zip
Going back to the DPNews.doc file created by DP in the first posting, if
you
wanted to create the final native (XML) Word 2003 document you would
simply
use the command:
xsltproc -o Newfile.doc DPNews.doc
and it would create the file NewFile.doc which could be directly opened
by
Word without it ever being obvious that it waqs not created in Word. If
you
preferred you could leave the new file with the extension XML instead of
DOC. It is likely that on a Windows platform that it would be correctly
opened by Word as there is a processing instruction in the XML file that
tell the operating system that it is a Word file.
The above command would use the associated XSLT sheet which was specified
in
the DP XML file. If you wanted to use a different XSLT template you could
override the default by using the command
xsltproc -o Newfile.doc DPNews.xsl DPNews.doc
Some really cool things with this is that, if you use the STDOUT facility
from the new DP2.6Y you could transform a file like this:
dp myapp.str /EI=mylog.log | xsltproc (with or without the -o
NewFile.doc)
and it would take the output directly from DP and pipe it to the XSLT
processing utility and either save the results to a new file or send the
output to STDOUT.
This means for example that you could from a webserver merge the data
with a
templkate and deliver the result as a file download to the user's
browser.
Alol without ever having to have used Word to generate the document, note
of
course you need Word to create the template, and you need Word (or the
free
Word Viewer) to view the final document. To make it a file download
rather
than be read as a webpage all that is needed to do this is to add the
appropriate MIME header before the document is output ie
Content-disposition: attachment; filename=defaultfilename.doc
If you have created your own DPNewsletter, you have probably found that
embedded line breaks in a memo field were ignored when you merged them
with
Word, whereas in my original example the linebreaks appeared. This is
because I had edited the DPNews.xsl template file and added another
template
rule:
<xsl:template match="ns0:br">
<w:br/>
</xsl:template>
The "ns0:" is a namespace prefix that Word added to the document (and it
might be different values so watch out). This rule ways that when you
encounter a <br /> in the DP X<ML file, replace it in the word document
with
<w:br /> which is the Word 2003 instruction for a line break.
It would be possible to add rules to cover the bold, underlined or italic
however that is not quite so easy and I wanted to make the example as
quickly as possible so I omitted it.
My example only output a single customer's newsletter. If I wanted to
create
a number of customers' newsletters in one merge it would have been
possible
however I would have probably had to hand tune Word XSLT file. Instead of
just having to apply the root "dpnews" element to the whole document I
would
have had to apply the "customer" element to the whole document as well.
This
would have meant that within the document my context would have been set
at
the "customer" level so the "article" elements would not have been so
obvious. I would have had to had tune the XPath, so from within the
"customer" content I would have had to specify the XPath as either
"/dpnews/article" or perhaps "../article" (XPath's really do behave a
lot
like directory paths)
I hope this gives an insignt into merging DP and Word.
Regards
Brian