View Full Version : Using Word 2003 XML files
AllanS
11-26-2003, 07:50 AM
I don't want to use office interop because I've been burned by it many times. Besides, who wants to run Office on a server?
What I want to do is have a Word 2003 XML document file acting as a template (stored on the server), read in the XML, merge the XML stream with data and then redirect the data enhanced XML stream to the client's browser for viewing using Word 2003 at the client.
I am into my second day of researching this technique. All the examples I am finding use a Word object. Again, I do not want Word on the server.
I want to use only the XML tools that are available in .NET, like XPathNavigator, but the Word smart tags are a real pain. I am looking at using bookmarks to identify where the data is to be inserted.
Before I spend too much time on this, I figured it would be a good idea to find out if anyone else has tried this or has a better idea.
So, how about it???
Thanks.
Allan Sieker
Eureka, MO
Allan Sieker
Eureka, MO
Major_Disorder
11-27-2003, 03:45 AM
[b]quote:Originally posted by AllanS
I don't want to use office interop because I've been burned by it many times. Besides, who wants to run Office on a server?
What I want to do is have a Word 2003 XML document file acting as a template (stored on the server), read in the XML, merge the XML stream with data and then redirect the data enhanced XML stream to the client's browser for viewing using Word 2003 at the client.
I am into my second day of researching this technique. All the examples I am finding use a Word object. Again, I do not want Word on the server.
I want to use only the XML tools that are available in .NET, like XPathNavigator, but the Word smart tags are a real pain. I am looking at using bookmarks to identify where the data is to be inserted.
Before I spend too much time on this, I figured it would be a good idea to find out if anyone else has tried this or has a better idea.
So, how about it???
</blockquote id="quote"></font id="quote">
Hi Allan,
I've actually developed a solution for our company that uses xslt to transform an xml stream into a Word 2003 document. I found that the best way to do it was:
<ul>
Define your XML document schema for the data you want to represent in the Word Document
Map the schema into the Word document to create your XML enabled document
Once you've done this, populate the document with some test data and save the .doc file as .xml
Open the file in a good xml editor like XML Spy and add the content to a stylesheet replacing the data areas of the Word XML file with the correct transforms
Once you've completed the stylesheet you can test it with XML Spy and some test data to generate the Word XML document[/list]
Then just install the xslt file on the server, extract your xml data stream, transform it with the stylesheet and deliver to the client.
Worked for me hope it's useful.
Pete
XBOXRacing.net
Tournaments, Leagues & Prizes
AllanS
12-06-2003, 09:41 AM
The solution that I went to production with was to use Word 2003 mailmerge fields and save the document on the server as XML. A server routine then reads in the XML and does a replace on the mailmerge fields. Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation.
I can now delete Word from the server along with the security and interop headaches. And the response is much faster because the server does not have to load the Word object.
Allan Sieker
Eureka, MO
PhillyRob
12-10-2003, 09:03 AM
I am new to the whole 'WordXML' thing and have a similar problem where i want to keep the format of the document but replace the data contained within it with new data from a separate standard XML file. Is there not some way of manipulating the WordXML document and simply replace the values contained in the XML tags (which I inserted into the original Word document) with the values from a separate (standard) XML file? It just seems silly to be able to create this template kind of document which can output WordXML, but not be able to import XML into it?!
Major_Disorder
12-10-2003, 11:18 AM
PhillyRob,
Take a look at my solution above, that does exactly what you want i think.
Just define your WordML document and plug the wordxml into a stylesheet to transform your XML with..... simple.
XBOXRacing.net
Tournaments, Leagues & Prizes
Randy Brown
12-11-2003, 01:44 AM
Allan,
Can you post the code that apply's to:
"...Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation...."
I know this is easy, but I can't find the code to get it done. I think I've even written this before, but back in the oldschool ASP days. I'm creating Word files on the server via XML saved from Word 2003 and replacing my bookmarks with SQL data. The XML is sitting in an XmlDocument object. I want to stream this to the browser and give the user the ability to save as true word Doc, rather than save as XML on the server and redirect to this file.
So I guess my question/request is if you have any code that shows the streaming of this Word XML down to the browser from an XmlDocument object, I'd love to se it! Probably have to load into a XmlReader I'll bet...
Thanks Allan,
Randy Brown, MCP
Randy Brown
12-11-2003, 01:45 AM
Allan,
Can you post the code that apply's to:
"...Lastly, the resulting XML streams out to the browser where Word 2003 at the client will handle the presentation...."
I know this is easy, but I can't find the code to get it done. I think I've even written this before, but back in the oldschool ASP days. I'm creating Word files on the server via XML saved from Word 2003 and replacing my bookmarks with SQL data. The XML is sitting in an XmlDocument object. I want to stream this to the browser and give the user the ability to save as true word Doc, rather than save as XML on the server and redirect to this file.
So I guess my question/request is if you have any code that shows the streaming of this Word XML down to the browser from an XmlDocument object, I'd love to se it! Probably have to load into a XmlReader I'll bet...
Thanks Allan,
Randy Brown, MCP
Ok, so I am REALLY new to all this XML stuff -- just started learning ASP 3 months ago...didn't even know Office 11 had XML capabilities until today. So, my issue is similar to what has been asked above.
All I need to do is create a document in Word with pretty formatting, and then populate the document with records pulled from a back-end system. I have pulled the records from the SQL database into Access, and then used that to create the schema and XML. I also created the Word doc and used the schema to map where all the elements should go. But, how do I get the data into the Word doc??? I am looking for an "import" or "merge" or something similar in Word, but find nothing. So, the problem is that I can't get the records into the doc. Do I need a stylesheet or XSLT file??? If so, how do I create one?
Thanks!
Major_Disorder
12-15-2003, 12:11 PM
To stick a doc back to the client
Response.ContentType = "application/vnd.ms-word";
Response.Write("you word xml");
XBOXRacing.net
Tournaments, Leagues & Prizes
PhillyRob
12-16-2003, 05:01 AM
Val,
You could populate your word document direct from the Access Database you created by using mail merge fields thus avoiding this messy XML lark!
set wordObj = CreateObject("Word.Application")
wordObj.Documents.Add()
wordObj.Selection.InsertFile('location of .dot file') --> (with the merge fields set up)
wordObj.ActiveDocument.MailMerge.OpenDataSource('l ocation of .db file')
wordObj.ActiveDocument.MailMerge.Destination = 0
wordObj.ActiveDocument.MailMerge.Execute(False)
wordObj.ActiveDocument.SaveAs('name of merged file')
I got this working but the XML files that I am using are hierarchical rather than flat and I have been told to use the new XML facilities in word... It seems there are 2 ways of populating your WordXML file with new values:
1. Loop through the nodes of the XML file and replace the corresponding value in the WordXML doc as suggested by AllanS?
something like this?:
Dim WordNode, WordChildNode As Word.XMLNode
Dim XMLNode, ChildXMLNode As System.Xml.XmlNode
For Each XMLNode In DataXML.ChildNodes
For Each WordNode In TempDoc.XMLNodes
For Each WordChildNode In WordNode.ChildNodes
For Each ChildXMLNode In XMLNode.ChildNodes
If ChildXMLNode.Name = WordChildNode.BaseName Then WordChildNode.Text = ChildXMLNode.InnerText
Next
Next
Next
Next
I found this gets messy when the XML is hierarchical (recordsets inside records?) and if you need to use tables to display the data....
2. Create (somehow) an XSLT with some sort of replace command (i am new to XSLTs also) which will sort of say 'get the value in the WordXML doc and replace it with the value in the dataonlyXML doc'. I am getting very confused on how to do this (and also not sure on how the hierarchical XML + tables will fit in) :S .... see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_wd2003_ta/html/odc_wdxmlom.asp and the other related pages.
If anyone manages to figure out what i'm talking about please reply with some miracle code!?!!
Thanx
Phil (a currently very stressed programmer)
Major_Disorder
12-16-2003, 09:10 AM
I must admit that the XSLT approach is probably not ideal if you're new to XSL & transformations but it doesn't require a server Word License (good) , it's easy to maintain if designed correctly as it doesn't require a re-compile should there be a change in formats.
Ok here's a quick sample to get you going.....
[quote]
<MyXMLSource>
<mytext>I'vecomefromthesourceXMLdocument</mytext>
</MyXMLSource></CODE>
XBOXRacing.net
Tournaments, Leagues & Prizes
Major_Disorder
12-16-2003, 09:15 AM
Here's the rest...
Ok the bit below is a stylesheet to generate your Word Document. You'll need to save it as a .xsl file in the same directory as the XML data file I just posted.
The apply the transformation to the source XML, just use XMLSpy for testing purposes and you should see a nice new WordXML document generated.
This is a VERY simple example but shows the process needed. I've successfully created multi-megabyte documents using this approach with a lot of nested data sets.
If you want a good maintainable design, then look at using xsl:template & xsl:applytemplates to split up your stylesheet.
[quote]
<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"xmlns:v="urn:schemas-microsoft-com:vml"xmlns:w10="urn:schemas-microsoft-com:office:word"xmlns:SL="http://schemas.microsoft.com/schemaLibrary/2003/core"xmlns:aml="http://schemas.microsoft.com/aml/2001/core"xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"xmlns:st1="urn:schemas-microsoft-com:office:smarttags"xml:space="preserve"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"exclude-result-prefixes="fo">
<xsl:outputomit-xml-declaration="yes"method="xml"indent="yes"/><xsl:templatematch="/"><xsl:processing-instructionname="mso-application"><xsl:text>progid="Word.Document"</xsl:text></xsl:processing-instruction>
<w:wordDocumentxmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"xmlns:v="urn:schemas-microsoft-com:vml"xmlns:w10="urn:schemas-microsoft-com:office:word"xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"xmlns:aml="http://schemas.microsoft.com/aml/2001/core"xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"xmlns:o="urn:schemas-microsoft-com:office:office"xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"w:macrosPresent="no"w:embeddedObjPresent="no"w:ocxPresent="no"xml:space="preserve">
<o:DocumentProperties>
<o:Title>Yourdocumenttitle</o:Title>
<o:Author>MajorD</o:Author>
<o:LastAuthor></o:LastAuthor>
<o:Revision>1</o:Revision>
<o:TotalTime>0</o:TotalTime>
<o:Created>2003-12-16T16:54:00Z</o:Created>
<o:LastSaved>2003-12-16T16:54:00Z</o:LastSaved>
<o:Pages>1</o:Pages>
<o:Words>4</o:Words>
<o:Characters>24</o:Characters>
<o:Company></o:Company>
<o:Lines>1</o:Lines>
<o:Paragraphs>1</o:Paragraphs>
<o:CharactersWithSpaces>27</o:CharactersWithSpaces>
<o:Version>11.5604</o:Version>
</o:DocumentProperties>
<w:fonts>
<w:defaultFontsw:ascii="TimesNewRoman"w:fareast="TimesNewRoman"w:h-ansi="TimesNewRoman"w:cs="TimesNewRoman"/>
</w:fonts>
<w:styles>
<w:versionOfBuiltInStylenamesw:val="4"/>
<w:latentStylesw:defLockedState="off"w:latentStyleCount="156"/>
<w:stylew:type="paragraph"w:default="on"w:styleId="Normal">
<w:namew:val="Normal"/>
<w:rPr>
<wx:fontwx:val="TimesNewRoman"/>
<w:szw:val="24"/>
<w:sz-csw:val="24"/>
<w:langw:val="EN-GB"w:fareast="EN-US"w:bidi="AR-SA"/>
</w:rPr>
</w:style>
<w:stylew:type="character"w:default="on"w:styleId="DefaultParagraphFont">
<w:namew:val="DefaultParagraphFont"/>
<w:semiHidden/>
</w:style>
<w:stylew:type="table"w:default="on"w:styleId="TableNormal">
<w:namew:val="NormalTable"/>
<wx:uiNamewx:val="TableNormal"/>
<w:semiHidden/>
<w:rPr>
<wx:fontwx:val="TimesNewRoman"/>
</w:rPr>
<w:tblPr>
<w:tblIndw:w="0"w:type="dxa"/>
<w:tblCellMar>
<w:topw:w="0"w:type="dxa"/>
<w:leftw:w="108"w:type="dxa"/>
<w:bottomw:w="0"w:type="dxa"/>
<w:rightw:w="108"w:type="dxa"/>
</w:tblCellMar>
</w:tblPr>
</w:style>
<w:stylew:type="list"w:default="on"w:styleId="NoList">
<w:namew:val="NoList"/>
<w:semiHidden/>
</w:style>
</w:styles>
<w:docPr>
<w:vieww:val="print"/>
<w:zoomw:percent="100"/>
<w:doNotEmbedSystemFonts/>
<w:proofStatew:grammar="clean"/>
<w:attachedTemplatew:val=""/>
<w:defaultTabStopw:val="720"/>
<w:characterSpacingControlw:val="DontCompress"/>
<w:optimizeForBrowser/>
<w:validateAgainstSchema/>
<w:saveInvalidXMLw:val="off"/>
<w:ignoreMixedContentw:val="off"/>
<w:alwaysShowPlaceholderTextw:val="off"/>
<w:compat>
<w:dontAllowFieldEndSelect/>
<w:useWord2002TableStyleRules/>
</w:compat>
</w:docPr>
<w:body>
<wx:sect>
<w:p>
<w:r>
<w:t><xsl:value-ofselect="//mytext"/></w:t>
</w:r>
</w:p>
<w:sectPr>
<w:pgSzw:w="11906"w:h="16838"/>
<w:pgMarw:top="1440"w:right="1800"w:bottom="1440"w:left="1800"w:header="708"w:footer="708"w:gutter="0"/>
<w:colsw:space="708"/>
<w:docGridw:line-pitch="360"/>
</w:sectPr>
</wx:sect>
</w:body>
</w:wordDocument>
</xsl:template>
</xsl:stylesheet>
</CODE>
XBOXRacing.net
Tournaments, Leagues & Prizes
Major_Disorder
12-16-2003, 09:17 AM
Oh yeah the bit I didn't tell ya about.....
the xsl:value-of instruction takes the data field from the source XML and outputs it to the WordXML.
Check out google for a whole heap of tutorials on XSLT, it's worth the time investment.
XBOXRacing.net
Tournaments, Leagues & Prizes
PhillyRob
12-17-2003, 03:05 AM
Oh nice one - thanks for you help - it's a lot clearer now.
I've now got to write a program to generate the XSLT needed to
update the values in the WordXML doc using the values in the XMLData
file. Thing is tho, the XSLT doesnt need to be as complex your
example (i hope!), because the user will create the WordXML doc with
all the formatting etc, so all i need to write in the XSLT is a load
of replace statements with the values from the XML data file, and
then apply it to the wordXML doc to replace each of the values in
the WordXML file. The XMLData file contains more than one record
tho, so I need it to be like a mail merge and repeat the page over
and over as well.....I've got 2 days left to do it in.....Wish me
luck!!
vBulletin® ©Jelsoft Enterprises Ltd.