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__ == "__main__":
  dh = XMLSpecHandler(  )
  parser = make_parser(  )
  parser.setContentHandler(dh)

  print "<html><head>"
  print "<style type='text/css'>"
  print "body { font-family: sans-serif; }"
  print "</style>"
  print "</head><body>"
  parser.parse(sys.stdin)
  print "</body></html>"

You can download the XML specification as an XML document from the W3C web site (http://www.w3.org/TR/). You can then run the script in Example B-1 against it to generate very simple HTML from the command line:

$[chris@spindle]> python gen-td.py < XMLSpec.py > termdef.html

If you load the termdef.html file in your web browser, you’ll see all of the term definitions from the XML Specification neatly presented in simple HTML. Figure B-1 shows the termdef.html output running in a browser.

termdef.html loaded in a browser
Figure B-1. termdef.html loaded in a browser

One note: the architects of the XML version of the specification didn’t neatly keep the full text of each definition within the confines of the termdef tags. So, a little human intervention was necessary to truly present them accurately in this text. The definitions presented here are a subset of what appears in the specification, as definitions related only to the specification itself (and not to XML) have been removed, and the list has been alphabetized to be more human-friendly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.69