Cocoon初步 - 从XML到HTML和PDF
from: http://www.javaresearch.org/forum/thread.jsp?column=20&thread=4103
本文介绍了如何利用Cocoon将XML文档发布成HTML和PDF。通过这篇文章你将具体实践到如何构建一个简单的Cocoon管道,如何使用XSLT将XML文档转换成HTML或者PDF,这样您就可以更好的理解Cocoon这个发布框架实际上是如何工作的。
几点说明:
- 在整篇文章里会经常引用$TOMCAT_HOME环境变量,他是指Tomcat 应用程序安装的目录。
- 为了测试程序,您的系统需要安装Cocoon。本文的示例在Cocoon 2.0.3 版测试通过,不过他可以在任何2.x版本里正常工作。
- 这里假定Cocoon是标准安装的情况,访问 http://localhost:8080/cocoon/mount/ 会显示一个标题为"Directory Listing of mount"的页面。
- 您必须能够在$TOMCAT/webapps/cocoon/mount 目录下创建和编辑xml文件。
按下面的步骤开始我们的示例:
1、在mount下创建工作目录
在 $TOMCAT/webapps/cocoon/mount目录下,创建一个新的目录 html-pdf。 我们的示例用到的所有文件都放在这里。
访问 http://localhost:8080/cocoon/mount/ ,页面里会显示出我们新建的目录html-pdf。
2、创建XML示例文档
我们使用两个简单的XML文件做为数据源。以后你可能会使用实际的XML或者数据库等作为数据源。
在 html-pdf目录下,创建下面的两个文件 pageOne.xml 和 pageTow.xml。
pageOne.xml的内容:
[pre]<?xml version="1.0" encoding="iso-8859-1"?>
<page>
<title>This is the pageOne.xml example</title>
<s1 title="Section one">
<p>This is the text of section one</p>
</s1>
</page>[/pre]
pageTwo.xml的内容:
[pre]<?xml version="1.0" encoding="iso-8859-1"?>
<page>
<title>This is the pageTwo.xml example</title>
<s1 title="Yes, it works">
<p>Now you're hopefully seeing pageTwo in HTML or PDF</p>
</s1>
</page>[/pre]
如果您使用的是 Unix/Linux系统,请注意文件名的大小写。
不要在XML文件的开头出现空格。<?xml... 指令必须是文件的开始。
3、创建用来生成HTML的XSLT
在Cocoon里常用的生成HTML的方法就是使用XSLT来选择并且转换输入文档的适当的元素。
把下面这个 doc2html.xsl 拷贝到 html-pdf 目录下,使其和上面两个XML文档在一起。
doc2html.xsl 的内容:
[pre]<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- generate HTML skeleton on root element -->
<xsl:template match="/">
<html>
<head>
<title><xsl:apply-templates select="page/title"/></title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<!-- story is used later by the Meerkat example -->
<xsl:template match="p|story">
<p><xsl:apply-templates/></p>
</xsl:template>
<!-- convert sections to HTML headings -->
<xsl:template match="s1">
<h1><xsl:apply-templates select="@title"/></h1>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet> [/pre]
4、创建站点地图
现在我们已经有了要发布的文档和用来将其转换成HTML的XSLT。接下来我们要做的就是在Cocoon管道里将其连接到一起,然后,站点地图就可以通过具体的请求来选择管道。
要告诉Cocoon如何处理来自html-pdf的请求,您需要拷贝下面这个sitemap.xmap文件到html-pdf 目录下。
sitemap.xmap 文件内容:
[pre]<?xml version="1.0" encoding="iso-8859-1"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<!-- use the standard components -->
<map:components>
<map:generators default="file"/>
<map:transformers default="xslt"/>
<map:readers default="resource"/>
<map:serializers default="html"/>
<map:selectors default="browser"/>
<map:matchers default="wildcard"/>
</map:components>
<map:pipelines>
<map:pipeline>
<!-- respond to *.html requests with
our docs processed by doc2html.xsl -->
<map:match pattern="*.html">
<map:generate src="{1}.xml"/>
<map:transform src="doc2html.xsl"/>
<map:serialize type="html"/>
</map:match>
<!-- later, respond to *.pdf requests with
our docs processed by doc2pdf.xsl -->
<map:match pattern="*.pdf">
<map:generate src="{1}.xml"/>
<map:transform src="doc2pdf.xsl"/>
<map:serialize type="fo2pdf"/>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>[/pre]
上面文件中很重要的一点就是 <map:match>元素,他用来告诉 Cocoon 如何处理这里路径里的以.html结尾的请求。
5. 测试 HTML 发布
到这时,我们应该能看到HTML页面的结果了。
- 访问 http://localhost:8080/cocoon/mount/html-pdf/pageOne.html 页面会显示出大号字体的"Section one" 。
- 访问 http://localhost:8080/cocoon/mount/html-pdf/pageTwo.html 页面会显示出大号字体"Yes it works"。
如果没有正常工作,您需要检测一下上面的各步骤,如果还找不到错误的原因的话,您可以到$TOMCAT/webapps/cocoon/WEB-INF/logs 目录查看Cocoon的日志。Cocoon的日志有多个文件,您只需要查看那个当错误发生时大小给变了的日志文件,从中找出线索。
上面的站点地图里已经包括了PDF发布的配置。但是这项功能此时并不能正常工作,因为我们还没有创建转换成PDF所需要的XSLT。
6、创建转换PDF用的XSLT
PDF文档是由XSL-FO文档(包含一些特定的页面描述词的XML文档)来生成的。由Cocoon 的 PDF 序列化器来完成实际的转化。
将下面的doc2pdf.xsl 拷贝到 html-pdf目录下:
doc2pdf.xsl
[pre]<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
>
<!-- generate PDF page structure -->
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="page"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm"
>
<fo:region-before extent="3cm"/>
<fo:region-body margin-top="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
<fo:page-sequence-master master-name="all">
<fo:repeatable-page-master-alternatives>
<fo:conditional-page-master-reference
master-reference="page" page-position="first"/>
</fo:repeatable-page-master-alternatives>
</fo:page-sequence-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="all">
<fo:flow flow-name="xsl-region-body">
<fo:block><xsl:apply-templates/></fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- process paragraphs -->
<xsl:template match="p">
<fo:block><xsl:apply-templates/></fo:block>
</xsl:template>
<!-- convert sections to XSL-FO headings -->
<xsl:template match="s1">
<fo:block font-size="24pt" color="red" font-weight="bold">
<xsl:apply-templates select="@title"/>
</fo:block>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>[/pre]
因为这个文件在我们前面的站点地图中已经引用,所以我们不需要再在sitemap.xmap做任何配置了。
7、 测试 PDF 发布
这时我们应该能看到PDF的结果了。
- 访问 http://localhost:8080/cocoon/mount/html-pdf/pageOne.pdf 会显示大号字体红色的"Section one"。
- 访问 http://localhost:8080/cocoon/mount/html-pdf/pageTwo.pdf 会显示大号字体红色的 "Yes it works" 。
到这里,您应该了解其实在Cocoon中发布HTML和PDF不是多难的事情。更为美妙的是当我们有大量的XML文档需要发布时,我们只需要编写两个XSLT文件来将其转换成特定的格式。而当我们需要改变发布文档的外观时,我们只需要修改那两个相应的XSLT,我们并不需要去接触那些源文档。
- 作者: wuxh 2004年09月11日,星期六 19:09:36

浙公网安备 33010602011771号