[XML-SIG] PyXML vs. stdlib xml.sax - SAX implementation/comparisonmatrix

Fredrik Lundh fredrik at pythonware.com
Thu Nov 17 20:06:15 CET 2005

Mike Brown wrote:

> If someone with a few hours to kill would be willing to generate some XSLT to
> render it, that would be great.

how about 20 minutes and Python ?

import elementtree.ElementTree as ET

doc = ET.parse("sax-in-python.xml")

class NS:
    def __init__(self, uri):
        self.uri = uri
    def __getattr__(self, tag):
        return self.uri + tag
    def __call__(self, path):
        return "/".join(getattr(self, tag) for tag in path.split("/"))

ns = NS("{http://pyxml.sourceforge.net/SAX-support-matrix/2005/11}")

# get implementation map
implementations = {}
for family in doc.findall(ns("implementations/package-family")):
    for set in family.findall(ns("package-set")):
        for item in set:
            implementations[item.text] = family.get("name"), set.get("name")

def line(indent, *args):
    print "<tr>",
    if len(args) == 1:
        print "<td colspan=3>",
        print "<td>",
    print "&nbsp;&nbsp;"*indent, "</td><td>".join(args), "</td>",
    print "</tr>"

print """\
    <title>SAX support in Python</title>
    <style type="text/css">
      th { text-align: left }
      th, td { background-color: #EEE; color: 000; }
      body { font-family: sans-serif }
  <body lang="en">
  <h2>SAX support in Python</h2>

# process technology grid
print "<table>"
for technology in doc.findall(ns.technology):
    line(0, technology.get("name").capitalize())
    thisgroup = None
    for group in technology:
        if group.tag != thisgroup:
            line(1, group.tag.split("}")[1].capitalize())
            thisgroup = group.tag
        line(2, group.get("type").capitalize())
        for item in group:
            name = item.get("name")
            implementation = item.findtext(ns.implementation)
            lib = implementations.get(implementation, ("", ""))
            line(3, name, lib[0], implementation)
print "</table>"

print """\

tweak as necessary.


More information about the XML-SIG mailing list