Generating a Word document from XML

Source: Internet
Author: User
Tags argumentlist xsl xslt

XML Generation Word Summary

The basic steps for using XML to generate word are clearly explained in the "Using XSLT to transform XML data to form Word documents export. doc". But the details did not say, so it took two days to finally succeed. Here are some of the key points I encountered in using the process:


You must use Word 2003.

1. The following example shows:


Simple XML and the corresponding XML framework

2. Create 03 versions of Word document LOG_03.DOC and O7 versions of Word documents Log_07.docx, respectively, and apply the LOG.XSD framework generated above


Save separately as Log_03_doc.xml,log_07_doc.xml.

3. Generate XSLT files Log_03.xslt and log_07.xslt with Log_03_doc.xml,log_07_doc.xml.


The above dialog box pops up when Log_03_doc.xml generates LOG_03.XSLT, just select the namespace specified in the XML.

Log_07_doc.xml generated log_07.xslt will pop up the above dialog box, that is, there is no XML specified in the namespace, all regardless of the choice, the resulting XSLT is not what we want.

Do not choose other namespace

Log_03_doc.xml generate LOG_03.XSLT will pop up the above dialog box, do not casually choose other namespace. The resulting XSLT


If you select another namespace, the resulting XSLT is not what we want. Generate XSLT

Obviously not the same as above.

Insert Picture

To implement inserting a picture, you need to modify Log_03.doc.

Regenerate the log_03.xslt, and modify the <ns0:image> node.

1. Delete the entire V:shapetype node

2. Delete the data from the <w:bindata node and add <xsl:value-ofselect= "." />

After modification

3, modify the V:shape node, remove the style attribute, add the XSL node, as follows:


4, remove the W:pict node behind the band <xsl:value-ofselect= "." All nodes of the/>

The following code is removed from the example:

<w:r>

<w:t>

<xsl:value-of select= "."/>

</w:t>

</w:r>

If you don't do the fourth step, the binary code for the picture appears in Word.

Example:

qdomdocument Dom;

qdomprocessinginstruction proc =

Dom. createprocessinginstruction (

"xml",

"version=\" 1.0\ "standalone=\" no\ ""

);

Dom. appendchild (proc);

qdomelement r =dom. createelement ("Log");

Dom. appendchild (R);

R. SetAttribute ("xmlns","logs");

qdomelement Usernode =dom. createelement ("user");

R. appendchild (usernode);

Qdomtext Usertextnode =dom. createTextNode ("John");

Usernode. appendchild (usertextnode);

qdomelement Loginnode =dom. createelement ("Login");

R. appendchild (loginnode);

Qdomtext Logintextnode =dom. createTextNode ("2015-01-1811:10:12");

Loginnode. appendchild (logintextnode);

qdomelement Logoffnode =dom. createelement ("Logoff");

R. appendchild (logoffnode);

Qdomtext Logofftextnode =dom. createTextNode ("2015-01-1813:10:12");

Logoffnode. appendchild (logofftextnode);

qdomelement Imagenode =dom. createelement ("image");

R. appendchild (imagenode);

QString PNG;

FILE*fpng =fopen("2.jpg","RB");

if ( fpng )

{

encode (fpng,png, 0);

fclose (fpng );

}

Imagenode. SetAttribute ("Style","width:300pt;height:300pt" );

Char Szbuffer [256];

//sprintf (szbuffer, "wordml://5.png");

Imagenode. SetAttribute ("Key","2.jpg" );

Qdomtext Logonnodetextnode =dom. createTextNode (png);

Imagenode. appendchild (logonnodetextnode);

QString XML =dom. toString ();

QFile file ("Log.xml");

if (file. Open (qiodevice::WriteOnly))

{

Qtextstream Output (&file);

output. Setcodec (qtextcodec::codecforname("UTF-8"));

//qstringcontent = dom.tostring ();

output<< XML;

file. Close ();

}

QString src ="Msxsl.exe";

QString Src1 ="Log.xml";

QString src2 ="Log_03.xslt";

QString src3 =QString("122") +". Doc";

qprocess*proce =newqprocess();

qstringlist argumentlist;

argumentlist. Append (src1 );

argumentlist. Append (src2 );

argumentlist. Append ("-O");

argumentlist. Append (src3 );

proce-start(src,argumentlist );

BOOL ret =proce-waitforstarted(5000);

Generating a Word document from XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.