Better colour-scheme.
[dom-editor.git] / Dome / to_html.py
blobf4044a7d63ae4c3d2403e61987862e416cd68891
1 # Due to a shocking number of bugs and incompatibilities between PyXML and 4Suite,
2 # this actually seems to be the easiest way to convert a XML document to HTML!
4 # (note: moved to XHTML now anyway)
5 # Validates the output.
7 import sys
8 from xml.dom.html import HTMLDocument
9 from Ft.Xml.cDomlette import implementation
10 from Ft.Xml.Xslt.Processor import Processor
11 from Ft.Xml import InputSource
12 doc = implementation.createDocument(None, 'root', None)
13 proc = Processor()
14 from cStringIO import StringIO
16 # The HTML writer adds some header fields, so strip any existing ones out or we'll get
17 # two lots...
19 stream = StringIO('''
20 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
21 xmlns:h="http://www.w3.org/1999/xhtml">
22 <xsl:output method="xml" encoding="utf-8"
23 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
24 doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
26 <xsl:template match='/h:html/h:head/h:meta[@name="generator"]' priority='2'/>
27 <xsl:template match='/h:html/h:head/h:meta[@http-equiv="Content-Type"]'/>
29 <xsl:template match='@*|node()'>
30 <xsl:copy>
31 <xsl:apply-templates select='@*'/>
32 <xsl:apply-templates/>
33 </xsl:copy>
34 </xsl:template>
36 <xsl:template match='h:*'>
37 <xsl:element name='{local-name(.)}' namespace='http://www.w3.org/1999/xhtml'>
38 <xsl:apply-templates select='@*'/>
39 <xsl:apply-templates/>
40 </xsl:element>
41 </xsl:template>
43 </xsl:stylesheet>
44 ''')
45 proc.appendStylesheet(InputSource.InputSource(stream))
47 def to_html(doc):
48 import os, rox
49 import traceback
50 data = proc.runNode(doc, None, ignorePis = 1)
51 cin, cout = os.popen4('xmllint --postvalid --noout -')
52 cin.write(data)
53 cin.close()
54 results = cout.read()
55 if results.strip():
56 rox.alert(results)
57 return data