Back to index

plone3  3.1.7
xml.py
Go to the documentation of this file.
00001 # ganked from zopt and sfive
00002 import os
00003 from StringIO import StringIO
00004 USELIBXML = False
00005 
00006 slug = """ <div><some tag="true">
00007                <other />            </some>
00008      </div>
00009      """
00010 xslt = """
00011 <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
00012   <xsl:output method="html" indent="yes"/>
00013   <xsl:strip-space elements="*"/>
00014   <xsl:template match="@*|node()">
00015     <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
00016   </xsl:template>
00017 </xsl:stylesheet>
00018 """
00019 
00020 #import libxml2, libxslt
00021 xsltfile = os.path.join(os.path.dirname(__file__), 'strip.xsl')
00022        
00023 #use stylesheet as global
00024 
00025 try:
00026     import libxml2, libxslt
00027     styledoc=libxml2.parseFile(xsltfile)
00028     style=libxslt.parseStylesheetDoc(styledoc)
00029 
00030     def libstrip(text):
00031         """
00032         strip out whitespace
00033         >>> print xstrip(slug)
00034         <div><some tag="true"><other></other></some></div>
00035         ...
00036         """
00037         encoding = 'UTF-8'
00038         INDENT = True
00039         try:
00040             doc = libxml2.parseDoc(text)
00041             res = style.applyStylesheet(doc, None)
00042             # XXX: this raises an unhandled c exception
00043             #res = style.saveResultToString(resdoc)
00044             out = res.serialize(encoding=encoding, format=INDENT)
00045         finally:
00046             try: doc.freeDoc()
00047             except: pass
00048             try: res.freeDoc()
00049             except: pass
00050         return out
00051 
00052 except ImportError:
00053     libstrip = None
00054 
00055 
00056 def lxmlstrip(text):
00057     """
00058     strip out whitespace
00059     >>> print xstrip(slug)
00060     <div><some tag="true"><other></other></some></div>
00061     ...
00062     """
00063     if not text:
00064         return ''
00065     
00066     from lxml import etree
00067     xslt_doc = etree.XML(xslt)
00068     stripper = etree.XSLT(xslt_doc)
00069     doc = etree.XML(text)
00070     result = stripper(doc)
00071     val = str(result)
00072     return val
00073 
00074 if libstrip and USELIBXML:
00075     xstrip = libstrip
00076 else:
00077     xstrip = lxmlstrip
00078 
00079 import unittest
00080 from zope.testing import doctest
00081 optionflags = doctest.REPORT_ONLY_FIRST_FAILURE | doctest.ELLIPSIS
00082 def test_suite():
00083 
00084     return unittest.TestSuite((
00085         doctest.DocTestSuite('xml', optionflags=optionflags)
00086         ))
00087 
00088 if __name__=="__main__":
00089     unittest.TextTestRunner().run(test_suite())