XML technologies / XML / Python & XML / More Python XML tools

(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)

This page introduces further ways to work with Python and XML not covered in detail in the book. Python & XML focuses on popular API standards, since these are most likely to appear across environments, languages and jobs. Working with XML requires understanding DOM. Python happens to offer all the standard APIs. Hence the book focuses on DOM, XPath, XSLT, SOAP and others.

Here we examine alternatives for XML in Python. Some provide equivalent functionality to tools used elsewhere, but with implementations better suited to different contexts.

Pyxie

Sean McGrath’s Pyxie is available at SourceFourge.net: Pyxie and uses the line-oriented PYX notation. PYX and Pyxie offer an alternative to SAX and DOM, optimized according to its author for pipeline processing, where one application’s output feeds the next. This idiom is common in Unix tools and also used on Windows, though less in end-user tools.

Pyxie parses XML into the line-oriented PYX format, emitting signals according to document content. Like SAX, it is event-based, but writes PYX events to standard output instead of invoking callback interfaces. Other text tools, such as grep, sed and awk can process PYX, or it can feed text-handling scripts written in Python or Perl.

PYX emits separate lines for different kinds of markup. Consider this XML:

<Book>
  <Name>Python und XML</Name>
  <Publisher>O’Reilly Verlag</Publisher>
</Book>

Pyxie or another PYX processor converts it to:

(Book
-\n
(Name
-Python und XML
)Name
-\n
(Publisher
-O’Reilly Verlag
)Publisher
-\n
)Book

Each document construct gets its own line, convenient for Unix-style command-line tools. Each line starts with a symbol identifying node type:

(     A left parenthesis marks element starts.

)     A right parenthesis marks element ends.

A     Uppercase A marks attributes.

-     A hyphen or minus marks character data.

?     A question mark marks processing instructions.

These symbols do not cover all XML constructs. There is no support for CDATA sections, DTDs or comments, for example.

Our Unix administration experience suggests PYX’s line-oriented markup is invaluable to people familiar with sed, awk and grep who need XML parsing without spending time coding to a parser’s requirements.

Another powerful feature is quick content inspection, offering grep-like searching. Line-oriented content is easily searched with tools such as grep, enabling complex document operations. For example, combine grep with PYX to use the options of grep on PYX output:

$> <PYX-erzeugendes-Kommando> | grep –v "Celsius"

If PYX contains temperatures such as “38 Grad Celsius”, this grep command ensures Celsius temperatures disappear from output. Such filters are harder with XPath and DOM. Conversely, PYX is probably less useful when converting SQL records to XML while respecting DTDs and namespaces. Complex tasks like that require DOM.

Python XML tools

XML Tools builds on PyXML with two GTK widgets, XmlTree and XmlEditor. They are available at logilab-xmltools for viewing and editing XML files respectively.

XmlTree displays XML as a tree familiar from Windows, KDE or GNOME file browsers. It is a GTK widget derived from GtkCTree.

Its API configures XML display and XPath tree filters, and provides a class for creating tree metadata.

The tree also offers configurable keys. An asterisk (*) recursively expands the selected node; a slash (/) collapses it.

XmlEditor is another GTK widget, editing XML and using XmlTree for display. Editable structure currently depends on a DTD, potentially changing to schemas later.

Besides a simple control API, XmlEditor offers a method named add_change_listener specifying a callback invoked whenever the editor’s buttons Apply or Ok are pressed.

XML Schema validator

The XML Schema validator, XSV, is available at Edinburgh Language Technology Group: XSV. Still evolving like XML Schema itself, it is updated frequently and appears to progress. XSV aims to be an early open-source schema-aware processor, complementing PyXML’s TREX validator and 4Suite’s Schematron validator.

Sab-pyth

Sab-pyth interfaces to the Sablotron XSLT processor written in C++. It exposes Sablotron APIs to Python. The interface is small and effective, with its own quirks and constants. Custom handlers can intercept processor-generated messages.

Sab-pyth is available online at python-org: SabPyth 1.0.1.

Redfoot

James Tauber and Daniel Krech developed this toolkit for RDF data (Resource Description Framework). Tools include an RDF parser and serializer, database, query API, convenient user interface and web server for viewing and editing database RDF. Sample Redfoot applications are also available.

More information, implementation and complete documentation are available at GitHub: redfoot.

XML components for Zope

Zope is an open-source application server written in Python and C by Zope Corporation. Its implementation and details are on the Zope users’ website. Although not explicitly an XML project, it offers interesting XML components for building websites.

Parsed XML

Parsed XML is an optional component storing persistent DOM in Zope’s object database. Started by Karl Anderson and now led by Martijn Pieters, it uses a DOM implementation largely written by coauthor Fred L. Drake, Jr.

Page Templates

Page Templates are another optional component presenting the results of website business-logic operations. The template language lets designers edit templates with graphical tools without breaking links to programmers’ business logic. Templates may use HTML, XML or XHTML. Evan Simpson leads the project and contributed much Zope component expertise. Coauthor Fred L. Drake, Jr. and Guido van Rossum contributed many non-Zope-specific parts.

Online resources

The Python/XML community gathers around the Python XML Special Interest Groups, abbreviated XML-SIG.

As with most Python SIGs, activity mainly takes place on a mailing list. XML-SIG’s page provides list details, archives and a subscription form.

Besides maintaining PyXML, used throughout this book, XML-SIG maintains the Python/XML Topic Guide, surveying Python’s XML tools and linking to further online and printed resources.

  

 

 

 

back to top