1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>10. How to use XML/XSL with CherryPy</title>
<META NAME="description" CONTENT="10. How to use XML/XSL with CherryPy">
<META NAME="keywords" CONTENT="howto">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="howto.css" type='text/css'>
<link rel="first" href="howto.html">
<link rel="contents" href="contents.html" title="Contents">
<LINK REL="next" HREF="node12.html">
<LINK REL="previous" HREF="node10.html">
<LINK REL="up" HREF="howto.html">
<LINK REL="next" HREF="node12.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node10.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node12.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node10.html">9. How to use</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node12.html">11. How to use</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="node11.html#SECTION0011100000000000000000">10.1 Introduction</a>
<LI><A href="node11.html#SECTION0011200000000000000000">10.2 Prerequisite</a>
<LI><A href="node11.html#SECTION0011300000000000000000">10.3 Using the XML/XSL package from CherryPy</a>
</ul>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION0011000000000000000000">
10. How to use XML/XSL with CherryPy</A>
</H1>
<P>
<H1><A NAME="SECTION0011100000000000000000">
10.1 Introduction</A>
</H1>
With CherryPy, you can use python standard datatypes (lists, dictionaries, ...) to store your data, and then you can use
masks (written in CHTL) to convert that data into HTML.
<P>
However, this can also be done with XML and XSL. I personally think that using python types directly, and then writing
CHTL masks is much easier and straightforward than XML/XSL, but there might be several reasons why you'd want to use XML/XSL:
<UL>
<LI>You're getting your data from some other application and that data comes in XML (which is likely to
happen, since XML/XSL is now a standard).
</LI>
<LI>Your website is a multi-million contract with a big company, and they want to hear words like "XML" or "XSL", because
it makes them feel safe ...
</LI>
<LI>Your boss will let you use CherryPy, but only if you use XML/XSL, because it makes him feel safe ...
</LI>
</UL>
<P>
<H1><A NAME="SECTION0011200000000000000000">
10.2 Prerequisite</A>
</H1>
This HowTo uses the 4Suite module developed by Fourthought, Inc. I tested it with version 0.12.0a1 of 4Suite and version
2.1 of python, but other combinations would also probably work.
<P>
Also, several other XML/XSL modules are available for Python, and using them is also probably very easy.
<P>
Start by downloading 4Suite from <a class="url" href="http://4suite.org">http://4suite.org</a> and installing it on your machine.
<P>
You can test your installation by firing up the Python interpreter and typing:
<div class="verbatim"><pre>
>>> from Ft.Xml.Xslt.Processor import Processor
::: Using pDomlette
>>>
</pre></div>
If this doesn't work for you, then 4Suite is not correctly installed on your system.
<P>
<H1><A NAME="SECTION0011300000000000000000">
10.3 Using the XML/XSL package from CherryPy</A>
</H1>
<P>
Once you have 4Suite installed, is is very easy to use it from CherryPy. You can use regular masks to write your XSL
stylesheets, and you have several ways to generate your XML (using a function, a view or even a mask).
<P>
The following example is a simple example that demonstrates a basic XML/XSL transformation:
<P>
<div class="verbatim"><pre>
from Ft.Xml.Xslt.Processor import Processor
CherryClass XslTransform:
function:
def transform(self, xslStylesheet, xmlInput):
processor = Processor()
processor.appendStylesheetString(xslStylesheet)
return processor.runString(xmlInput, 0, {})
CherryClass Root:
view:
def index(self):
return xslTransform.transform(self.xslStylesheet(), self.xmlInput())
mask:
def xslStylesheet(self):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html><body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body></html>
</xsl:template>
</xsl:stylesheet>
def xmlInput(self):
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
</cd>
</catalog>
</pre></div>
<P>
How does it work ?
<UL>
<LI><var>xslStylesheet</var> is a mask containing the XSL stylesheet
</LI>
<LI><var>xmlInput</var> is a mask containing the XML input. In real life, this XML would probably be generated from a function,
based on data coming from a database or some other ressource.
</LI>
<LI>The <var>index</var> view just says to apply this XSL stylesheet to that XML input.
</LI>
<LI>The <var>XslTransform</var> CherryClass is just used to implement the <var>transform</var> function, to avoid having to repeat
these 3 lines of code for each transformation.
</LI>
</UL>
<P>
Here is the HTML document generated in this example:
<div class="verbatim"><pre>
<html>
<body>
<h2>My CD Collection</h2>
<table border='1'>
<tr bgcolor='#9acd32'>
<th align='left'>Title</th>
<th align='left'>Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
</tr>
</table>
</body>
</html>
</pre></div>
<P>
PS: Note that it seems that the 4Suite API has changed again in the latest releases, so you might have to tweak the above code if you're using a recent release.
<P>
<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node10.html"><img src="../icons/previous.gif"
border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A HREF="node12.html"><img src="../icons/next.gif"
border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
border="0" height="32"
alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
<td><img src="../icons/blank.gif"
border="0" height="32"
alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node10.html">9. How to use</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node12.html">11. How to use</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
|