[Tutor] lost
Erik Price
erikprice@mac.com
Mon, 18 Mar 2002 22:20:47 -0500
On Monday, March 18, 2002, at 09:00 AM, Karthik Gurumurthy wrote:
> I was wondering how to use it with python libraries. But have not been
> able
> to figure it out.
> Am not sure what python-xml SIG thinks about JAXP from Sun which tries
> to
> hide
> different parser implementation so that the code becomes portable across
> parsers.
>
> So i looked for similar names in the PyXML src :-( to make some sense
> out of
> it.
> I know it might sound stupid but the documentation is'nt helpful.
> There is no proper documentation for xslt stuff.
> Hope they will provide one soon.
Karthik,
Here's what the book has to say:
Essentially, the book's example of embedding an XSLT processor into a
Python program is a CGI script that takes two style sheets and an XML
document, and runs the appropriate style sheet on the document depending
on some user actions. Since it sounds like you don't need me to post
two XSLT sheets and an XML file to explain what's going on, I'll just
post the Python CGI program (but if you want the other stuff let me
know):
The book doesn't actually detail -how- to install 4XSLT but does say "as
explained earlier", so maybe I missed it, or maybe the best thing is to
go to their web site and follow their instructions (I haven't been there
yet but it's http://www.4suite.org/).
<BookContents source="Python & XML" author="Christopher Jones & Fred
Drake Jr" page="147-8">
#!/usr/local/bin/python
# xlst.cgi # sic, probably a typo
import cgi
import os
import sys
from xml.xslt.Processor import Processor
# parse query string & instantiate xslt proc
query = cgi.FieldStorage()
xsltproc = Processor()
print "Content-type: text/html\r\n"
mode = query.getvalue("mode", "")
if not mode:
print "<html><body>"
print "<p>No mode given</p>"
print "</html></body>" # sic, again
sys.exit()
if mode[0] == "show":
# run XML through simple stylesheet
xsltproc.appendStylesheetUri("story.xsl")
html = xsltproc.runUri("story.xml")
print html
elif mode[0] == "change":
# change XML source file, rerun stylesheet and show
newXML = '<?xml version="1.0"?>\n'
newXML += "\t<story>\n\t<title>"
newXML += quer.getvalue("title")[0] + "</title>\n"
newXML += "\t<body>\n"
newXML += query.getvalue("body")[0] + "\n\t</body>\n</story>\n"
fd = open("story.xml", "w")
fd.write(newXML)
fd.close()
# run updated XML through simple stylesheet
xsltproc.appendStylesheetUri("story.xsl")
html = xsltproc.runUri("story.xml")
print html
elif mode[0] == "edit":
# run XML through form-based stylesheet
xsltproc.appendStylesheetUri("edstory.xsl")
html = xsltproc.runUri("story.xml")
print html
</BookContents>
This is kind of out of context since I didn't copy the XML or XSLT
sheets -- they're very very simple, but if you want I can do those
tomorrow, just let me know.
Erik