<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jython Journeys &#187; xml</title>
	<atom:link href="http://jython.xhaus.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://jython.xhaus.com</link>
	<description>Notes about my work with jython and python</description>
	<lastBuildDate>Tue, 01 Dec 2009 19:37:22 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Transforming with XSLT on Google AppEngine and jython.</title>
		<link>http://jython.xhaus.com/transforming-with-xslt-on-google-appengine-and-jython/</link>
		<comments>http://jython.xhaus.com/transforming-with-xslt-on-google-appengine-and-jython/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 18:42:26 +0000</pubDate>
		<dc:creator>alan.kennedy</dc:creator>
				<category><![CDATA[google appengine]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jython]]></category>
		<category><![CDATA[cloud computing]]></category>
		<category><![CDATA[java scripting]]></category>
		<category><![CDATA[web technology]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://jython.xhaus.com/?p=171</guid>
		<description><![CDATA[I&#8217;m writing this post in response to a challenge. I wrote a post entitled Jython on Google AppEngine: Why bother?, which was a reply to a statement made by a Google engineer about not seeing the point of running jython on AppEngine. And a similar statement was repeated in a comment on that blog post [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m writing this post in response to a challenge. I wrote a post entitled <a href="http://jython.xhaus.com/jython-on-google-appengine-why-bother/">Jython on Google AppEngine: Why bother?</a>, which was a reply to a statement made by a Google engineer about not seeing the point of running jython on AppEngine. And a similar statement was repeated in a <a href="http://jython.xhaus.com/jython-on-google-appengine-why-bother/#comments">comment on that blog post</a> (i.e. &#8220;<i>the gist of what I intended to say was that while it was possible to run Jython on App Engine, I couldn’t think why you’d want to</i>.&#8221;)</p>
<p>So rather than get into a back-and-forth of yes-it-is-no-it-isnt-yes-it-is, I thought I&#8217;d reply with some simple code that demonstrates <b>something that cannot (currently) be done in cpython on AppEngine</b>, but is <b>easy with jython on AppEngine</b>: XSLT transforms. </p>
<p><span id="more-171"></span></p>
<p>So, without further ado, here is the jython source code for running an XSLT transform on Google AppEngine. It is implemented as a jython WSGI application, using xhaus.com&#8217;s <a href="http://modjy.xhaus.com">modjy WSGI gateway for java servlets</a> that was <a href="http://jython.xhaus.com/modjy-now-fully-integrated-into-jython/">contributed to the jython project a few months back</a>.</p>
<p>In order to get this code running, I suggest you download the <a href="http://downloads.xhaus.com/modjy_webapp_google_appengine/">modjy on google appengine demo</a> from our <a href="http://downloads.xhaus.com">downloads site</a>. In that download, there is a jython file called &#8220;demo_app.py&#8221;. Simply replace the entire contents of that file with the code below, and you&#8217;re good to go. If you need help with getting the modjy demo application working on Google AppEngine, see the documentation on the modjy wiki: <a href="http://opensource.xhaus.com/projects/modjy/wiki/ModjyGoogleAppEngine">Running jython WSGI applications on Google AppEngine, with modjy</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">__doc__ = <span style="color: #483d8b;">&quot;&quot;&quot;
A demonstration of how to run XSLT transforms on Google AppEngine with jython (not cpython).
Please feel free to use this code in whatever way you wish.
&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> java.<span style="color: black;">io</span> <span style="color: #ff7700;font-weight:bold;">import</span> StringReader, StringWriter
<span style="color: #ff7700;font-weight:bold;">from</span> javax.<span style="color: #dc143c;">xml</span>.<span style="color: black;">transform</span> <span style="color: #ff7700;font-weight:bold;">import</span> Transformer, TransformerFactory
<span style="color: #ff7700;font-weight:bold;">from</span> javax.<span style="color: #dc143c;">xml</span>.<span style="color: black;">transform</span>.<span style="color: black;">stream</span> <span style="color: #ff7700;font-weight:bold;">import</span> StreamResult, StreamSource
&nbsp;
xml_data = <span style="color: #483d8b;">&quot;&quot;&quot;
&lt;books&gt;
  &lt;book&gt;
    &lt;title&gt;2001 - A Space Odyssey&lt;/title&gt;
    &lt;author&gt;Arthur C Clarke&lt;/author&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;I, Robot&lt;/title&gt;
    &lt;author&gt;Isaac Asimov&lt;/author&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;title&gt;Kil'n people&lt;/title&gt;
    &lt;author&gt;David Brin&lt;/author&gt;
  &lt;/book&gt;
&lt;/books&gt;
&quot;&quot;&quot;</span>
&nbsp;
xslt_source = <span style="color: #483d8b;">&quot;&quot;&quot;
&lt;xsl:stylesheet
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
&gt;
&nbsp;
&lt;xsl:template match=&quot;books&quot;&gt;
&lt;html&gt;
  &lt;head&gt;&lt;title&gt;A list of books, transformed into html from xml using xslt and jython.&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;table border=&quot;1&quot; width=&quot;50%&quot; align=&quot;center&quot;&gt;
      &lt;caption&gt;A list of books, transformed into html from xml using xslt and jython.&lt;/caption&gt;
      &lt;tr&gt;&lt;th&gt;Title&lt;/th&gt;&lt;th&gt;Author&lt;/th&gt;&lt;/tr&gt;
    &lt;xsl:for-each select=&quot;book&quot;&gt;
      &lt;tr&gt;&lt;td&gt;&lt;xsl:value-of select=&quot;title/text()&quot;/&gt;&lt;/td&gt;&lt;td&gt;&lt;xsl:value-of select=&quot;author/text()&quot;/&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;/xsl:for-each&gt;
    &lt;/table&gt;
  &lt;/body&gt;
&lt;/html&gt;
&lt;/xsl:template&gt;
&nbsp;
&lt;/xsl:stylesheet&gt;
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> generate_html<span style="color: black;">&#40;</span>xml_data, xslt_source<span style="color: black;">&#41;</span>:
  transformer = TransformerFactory.<span style="color: black;">newInstance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">newTransformer</span><span style="color: black;">&#40;</span>StreamSource<span style="color: black;">&#40;</span>StringReader<span style="color: black;">&#40;</span>xslt_source<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  output_buffer = StringWriter<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  transformer.<span style="color: black;">transform</span><span style="color: black;">&#40;</span>StreamSource<span style="color: black;">&#40;</span>StringReader<span style="color: black;">&#40;</span>xml_data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, StreamResult<span style="color: black;">&#40;</span>output_buffer<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> output_buffer.<span style="color: black;">buffer</span>.<span style="color: black;">toString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> handler<span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>:
  result = generate_html<span style="color: black;">&#40;</span>xml_data, xslt_source<span style="color: black;">&#41;</span>
  start_response<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;200 Hoopy&quot;</span>, <span style="color: black;">&#91;</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">'content-type'</span>, <span style="color: #483d8b;">'text/html'</span><span style="color: black;">&#41;</span> <span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>result<span style="color: black;">&#93;</span></pre></div></div>

<p>Happy Transforming!</p>
]]></content:encoded>
			<wfw:commentRss>http://jython.xhaus.com/transforming-with-xslt-on-google-appengine-and-jython/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
