Cocoon is a servlet that allows you to separate web development in three different layers (content, style and logic).
Cocoon does not aim to simplify the creation of web content: in fact, it is harder to create XML/XSL content than it is to use HTML from the beginning. The advantages come on the long run, on site management, update, refining.
Let's start with the classic hello world file written in XML
<?xml version="1.0"?> <?xml:stylesheet href="hello.xsl" type="text/xsl"?> <page> <title>Hello World!</title> <content> <paragraph>This is my first XML/XSL file!</paragraph> </content> </page>Even if this page mimics HTML, it is helpful to note that there is no style information and all the styling and graphic part is placed in the XSL stylesheet indicated by the processing instruction
<?xml:stylesheet ... ?>
that is the proposed standard to map a stylesheet to a file.Since the XML syntax is pretty simple and straightforward, we won't go over it very much but we'll concentrate on the XSL side. Here is the referenced XSL stylesheet:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="page"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body bgcolor="#ffffff"> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="title"> <h1 align="center"> <xsl:apply-templates/> </h1> </xsl:template> <xsl:template match="paragraph"> <p align="center"> <xsl:apply-templates/> </p> </xsl:template> </xsl:stylesheet>XSL is both a rendering language as well as a document tree transformation language. Cocoon uses only the tree transformation part of XSL, the one capable of transforming any well-formed XML document into another well-formed one (in this case a well-formed HTML document). XSL transforms the document tree by applying "templates" to "patterns". Templates are document fragments that replace the searched patter.
For example, the following rule:
<xsl:template match="title"> <h1 align="center"> <xsl:apply-templates/> </h1> </xsl:template>is able to encapsulate the content of the "title" tag inside the HTML <h1> tag with center alignment. The "title" patter is stylized using the associated template. The
xsl:
namespace is used to indicate the valid xsl entities.The
<xsl:apply-templates/>
entity is the key to the knowledge of XSL. This entity is replaced, by the XSL processor, by all the output generated by the children of the matched pattern. In this case, the content of the title tag.In the following template it becomes clear what happens:
<xsl:template match="page"> <html> <head> <title><xsl:value-of select="title"/></title> </head> <body bgcolor="#ffffff"> <xsl:apply-templates/> </body> </html> </xsl:template>There are two XSL entities. The first is used to query the value of the patter "title" and second is used to continue the XSL evaluation of the children of the "page" pattern (in the give XML, these children are the patterns "title" and "content")
You should see a little problem here: how does the "paragraph" pattern receive its focus if there is no "content" template?
As a generic rule, the "*" pattern (meaning all entities) is implicitly associated with the empty template. This allows all the tree to be evaluated but only the entities matched with templates are rendered. This would allow the same XML file to have different scopes given different XSL stylesheets (i.e. an abstract stylesheet renders only the abstract of a document while a more complete stylesheet renders all the information contained)
The empty template may be visualized as
<xsl:template match="*"> <xsl:apply-templates/> </xsl:template>
The previous XML file was rendered exactly the same for every browser that requested it. Cocoon is able to discriminate between browsers, allowing the different stylesheets to be applied. For example, if we start with an XML file with
<?xml version="1.0"?> <?xml:stylesheet href="hello.xsl" type="text/xsl"?> <?xml:stylesheet href="hello.text.xsl" type="text/xsl" media="lynx"?>...Cocoon will be able to apply the hello.text.xsl stylesheet if the Lynx browser is requesting the page. This powerful feature allows you to design your content indipendently and to choose its presentation depending on the capabilities of the browser agent.
Note that you don't need to configure this behavior or to map URLs to stylesheets: the XML files contain all the information needed by Cocoon to evaluate them.
Copyright (c) 1999 The
Java Apache Project.
$Id: guide.html,v 1.1.1.1 1999/03/09 23:05:10 stefano Exp $
All rights reserved.