Cocoon est un "publishing framework" basé XML et écrit en Java
Philosophie de base = séparation des tâches:
Résumé: Cocoon/XSP permet de séparer contenu, "logique" et style.
(quasi-impossible avec Php, Jsp, Asp etc.)
Il faut faire un fichier *.xml (sxml à TECFA) et un fichier *.xsl (style)
Vous devez indiquer à Cocoon (selon l'exemple ci-dessous):
Fichier XML (extension = *.sxml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="VOTRE_FICHER_XSL.xsl" type="text/xsl"?>
<?cocoon-process type="xslt"?>
Fichier XSL (extension = .xsl):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
// doit aller DANS le template pour la racine XML !!!
<xsl:template match="VOTRE_RACINE">
<xsl:processing-instruction name="cocoon-format">type="text/html" </xsl:processing-instruction>
......
<?xml version="1.0"?>
<?xml-stylesheet href="ldap.xsl" type="text/xsl"?>
<?cocoon-process type="ldap"?>
<?cocoon-process type="xslt"?>
<page>
<ldap-defs>
<ldap-server name="tecfa">
<initializer> com.sun.jndi.ldap.LdapCtxFactory</initializer>
<ldap-serverurl> ldap://tecfa2.unige.ch:389</ldap-serverurl>
</ldap-server>
<ldap-querydefs name="standard" default="yes"/>
</ldap-defs>
<ldap-query server="tecfa" ldap-searchbase="o=tecfa.unige.ch" defs="standard">
givenname=Daniel
</ldap-query>
</page>
<?xml version="1.0"?>
<?xml-stylesheet href="hello-page-xslfo.xsl" type="text/xsl"?>
<?cocoon-process type="xslt"?>
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
version="1.0" >
<xsl:template match="page">
<xsl:processing-instruction name="cocoon-format">type="text/xslfo"
</xsl:processing-instruction>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<fo:layout-master-set>
.......
<?xml version="1.0"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="simple-page-html.xsl" type="text/xsl"?>
<xsp:page language="java"
xmlns:xsp="http://www.apache.org/1999/XSP/Core"
xmlns:util="http://www.apache.org/1999/XSP/Util"
<page>
<title>Good morning or good afternoon</title>
<p> It is <util:time format= "HH:mm, dd-MM-yyyy"/> </p>
<p> .... or <util:time format=""/> if you prefer. </p>
</page>
</xsp:page>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="VOTRE_SHEET.xsl" type="text/xsl"?>
<xsp:page language="java" xmlns:xsp="http://www.apache.org/1999/XSP/Core">
<racine_XML>
<xsp:logic>
du code Java (comme les scriptlets <% %> en JSP
</xsp:logic>
<xsp:expr>
une simple expression Java (comme les <%= %> en JSP )
</xsp:expr>
</racine_XML>
</xsp:page>
<xsp:page language="java" xmlns:xsp="http://www.apache.org/1999/XSP/Core">
<page>
<xsp:logic>
String choice = request.getParameter("choice ");
String choice2 = request.getParameter("choice2 ");
int score = 0;
if ((choice == null) || (choice2 == null)) {
<xsp:content> Please use the <a href="form.html">form</a> </xsp:content>
return;
} else score = Integer.parseInt(choice) + Integer.parseInt(choice2);
</xsp:logic>
<title>Form - XSP Demo </title>
<content>You entered <xsp:expr> choice</xsp:expr> and <xsp:expr> choice2</xsp:expr> . That makes <xsp:expr>score</xsp:expr>.</content>
</page>
</xsp:page>
<?xml version="1.0"?>
<?cocoon-process type="xsp"?>
<?cocoon-process type="xslt"?>
<?xml-stylesheet href="simple-taglib-html.xsl" type="text/xsl"?>
<xsp:page
language="java"
xmlns:xsp="http://www.apache.org/1999/XSP/Core"
xmlns:esql="http://apache.org/cocoon/SQL/v2" >
<page>
<title>Cocoon XSP ESQL TagLibs demo</title>
<author> <name>Daniel Schneider</name> </author>
<p> Shows some of Cocoon's SQL/XSP tag library. Also shows how to build a simple table with xsl </p>
<esql:connection>
<esql:driver> org.gjt.mm.mysql.Driver</esql:driver>
<esql:dburl> jdbc:mysql://tecfa.unige.ch/demo</esql:dburl>
<esql:username> nobody</esql:username>
<esql:password> </esql:password>
<esql:execute-query>
<esql:query> SELECT * FROM demo1 order by id;</esql:query>
<esql:results>
<ROWSET>
<esql:row-results>
<ROW>
<esql:get-columns/>
</ROW>
</esql:row-results>
</ROWSET>
</esql:results>
</esql:execute-query>
</esql:connection>
<p> See <a href="./">directory and appended README.html</a> for more information. </p>
</page>
</xsp:page>
<esql:results>
<ROWSET>
<esql:row-results>
<ROW>
<id><esql:get-int column="id"/></id>
<login><esql:get-string column="login"/></login>
<fullname><esql:get-string column="fullname"/></fullname>
<url><esql:get-string column="url"/></url>
<food><esql:get-string column="food"/></food>
</ROW>
</esql:row-results>
</ROWSET>
</esql:results>