Appendix B. XML Definitions

This appendix details the definitions spelled out in the XML Specification. These terms are used frequently in connection with the DOM, and with XML technology in general. All of the terms are defined here for reference purposes. This appendix should serve as a companion to the previous specification walkthrough from Chapter 2. In order to extract the XML terms and definitions out of the specification, we thought it appropriate to write an XML-processing script that operates on the XML version of the W3C Recommendation. For a good example of how to reveal the utility of Python and XML, use the program in Example B-1 to parse terms out of the specification’s XML.

Example B-1. gen-td.py—a script to print XML definitions

"""Generate HTML for terms and definitions directly from XML specification. XML source must come from standard input. """ import sys from xml.sax import ContentHandler from xml.sax import make_parser class XMLSpecHandler(ContentHandler): """ Class implements part of SAX API to pull term definitions out of the XML Specification source file. """ inTermDef = 0 def startElement(self, name, attrs): if name == "termdef": self.inTermDef = 1 self.strTermDefContents = "" print "<p><b>" + attrs.get('term', "") + "</b><br>" def characters(self, ch): if self.inTermDef: self.strTermDefContents += ch def endElement(self, name): if name == "termdef": self.inTermDef = 0 self.strTermDefContents += "</p>" print self.strTermDefContents # Main if __name__ == ...

Get Python & XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.