当前位置:编程学习 > C#/ASP.NET >>

XML文件到RTF和PDF的转换

答案:一. 序
在一些运用中,我们通常会把一些文本和配置信息转换成XML文件进行传输,修改,保存.特别是具有一定模板性质的文档用XML文件来实现其管理就显得相当的方便了.提供对于XML文件的操作的java API很多,诸于DOM,JDOM,Castor,SAX,XMLReader,XPath,XSLT等等. 具体的这些API的用法这里就不多提了. 当使用这些接口实现XML的操作后,对于有些文档而言最终必须呈现给用户看的还是我们通常所熟悉的WORD和PDF文档.我们这里就来看一下从一个XML文件到RTF和PDF文件转换的实现.





--------------------------------------------------------------------------------



二. 从XML到PDF
对于一个具有一定模板性质的XML文件,我们可以用FOP API来实现其到PDF的转换.

FOP需要fop.jar. 我们可以到http://xml.apache.org/fop/ 上获取和了解其用法.

以一个一般复杂的XML文件为例:

要转换XML文档 test.xml 如下:


<FeatureSRS title="SRS">
<introduction>
<objective>objective here</objective>
<scope>scope here</scope>
<responsibilities>responsibilities here</responsibilities>
<references>reference here</references>
<DAA>
<term>
term here
</term>
<definition>
definition here
</definition>
</DAA>
</introduction>
<generalDescription>
<featureName>
<summary>summary here</summary>
<breakdown>breakdown here</breakdown>
</featureName>
<requirement>
<content>
content here.
</content>
</requirement>
<requirement>
<content>
content2 here.
</content>
</requirement>
<featureInteractions>featureInteractions here</featureInteractions>
</generalDescription>
<strResources>
<strResource>
<estring>
estring here
</estring>
<resourceid>
resourceid here
</resourceid>
<rqmt>
rqmt here.
</rqmt>
</strResource>
</strResources>
</FeatureSRS>


对于这样一个XML文档,我们要将其转化成PDF格式必须建立一个XSL-FO文件,来定义对各element和value格

式的转换.

我们建立XSL-FO文件 test.xsl 如下:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
<!-- ========================= -->
<!-- root element: projectteam -->
<!-- ========================= -->
<xsl:template match="FeatureSRS">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simpleA4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="20pt" font-weight="bold" space-after="5mm" text-align="center">Cardiac Feature SRS
</fo:block>
<fo:block font-size="10pt">
<xsl:apply-templates/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- ========================= -->
<!-- child element: member -->
<!-- ========================= -->
<xsl:template name="introduction" match="introduction">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">1. Intruction</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.1 Objective</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="objective"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.2 Scope</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="scope"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.3. Responsibilities</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="responsibilities"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.4. References</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="references"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.5. Definitions, Acronyms, and Abbreviations</fo:block>
<fo:block font-size="10pt" font-weight="bold" space-after="5mm" margin-left="5mm">
<fo:table table-layout="fixed" border="2cm" background-color="#fff2d9" >
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="6cm"/>
<fo:table-body>
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:text>Term</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>Definition</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="DAA">
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:value-of select="term"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="definition"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template name="generalDescription" match="generalDescription">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">2. General Description</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.1. Feature Name</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-aft

上一个:XML中的实体引用符
下一个:探索 XML 的替代语法 —— 阅读和思考

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,