Link: start
Link: parent
Link: First page in set (first)
Link: Previous page (previous)
Link: Next page (next)
Link: Last page in set (last)
Link: A plain text version of this page (alternate)
Link: The XIST source of this page (alternate)
Advanced XIST
=============
Converter contexts, pool chaining, namespace extensions, conversion
targets, validation
=======================================================================================
Home > Python software > ll.xist > Advanced topics Text · XIST
Python softwarelist of projects
* ll.xistAn extensible XML/HTML generator
* ExamplesParsing/creating/modifying XML; Traversing XML
trees
* HowtoExplains parsing/generating XML files, XML
transformations via XIST classes and other basic
concepts.
* SearchingHow to iterate through XIST trees
* TransformationHow to transform XIST trees
* Advanced topicsPool chaining, converter contexts,
validation
* MiscellaneousExplains various odds and ends of XIST
* xscXIST core classes
* nsPackage containing namespace modules
* parseParsing XML
* presentScreen output of XML trees
* simsSimple schema validation
* xfindTree iteration and filtering
* cssCSS related functions
* scriptsScripts for text conversion and creating XIST
namespaces
* HistoryChangeLog for XIST
* InstallationHow to install and configure XIST
* MigrationHow to update your code to new versions of XIST
* Mailing listsHow to subscribe to the XIST mailing lists
* ll.ul4cA templating language
* ll.urlRFC 2396 compliant URLs
* ll.makeObject oriented make replacement
* ll.daemonForking daemon processes
* ll.sisyphusWriting cron jobs with Python
* ll.colorRGB color values and color model conversion
* ll.miscMisc utility functions and classes
* ll.orasqlUtilities for cx_Oracle
* ll.nightshadeServe the output of Oracle functions/procedures
with CherryPy
* ll.scriptsScripts for UL4 template rendering and URL handling
* AploraLogging Apache HTTP requests to an Oracle database
* PycocoPython code coverage
* DownloadLinks to Windows and Linux, source and binary
distributions
* Source codeAccess to the Mercurial repositories
Converter contexts
==================
Converter contexts can be used to pass around information in
recursive calls to the convert and mapped methods. A Converter
object will be passed in all calls, so this object is the place to
store information. However if each element, procinst and entity
class decided on its own which attributes names to use, name
collisions would be inevitale. To avoid this, the following system
is used.
When a class wants to store information in a converter, it has to
define a Context class (normally derived from the Context class of
its base class). The constructor must initialize the context
object to a initial state. You can get the context object for a
certain class by treating the converter as a dictionary with the
class (or an instance) as the key like this:
from ll.xist import xsc
class counter(xsc.Element):
· class Context(xsc.Element.Context):
· · def __init__(self):
· · · xsc.Element.Context.__init__(self)
· · · self.count = 0
· def convert(self, converter):
· · context = converter[self]
· · node = xsc.Text(context.count)
· · context.count += 1
· · return node
Defining and using a converter context
Chaining pools and extending namespaces
=======================================
When using pools it's possible to do some sort of "namespace
subclassing".
Registering a module in a pool not only registers the element,
procinst and entity classes in the pool for parsing, but each
attribute of the module (as long as it's weak referencable) is
available as an attribute of the pool itself:
from ll.xist import xsc
from ll.xist.ns import html
pool = xsc.Pool(html)
print pool.img
Pool attributes
This outputs foo
foo
For your own elements you can specify the content model too. This is done by setting the class attribute model inside the element class. model must be an object that provides a checkvalid method. This method will be called during parsing or publishing with the element as an argument. When invalid content is detected, the Python warning framework should be used to issue a warning. The module ll.xist.sims contains several classes that provide simple validation methods: Empty can be used to ensure that the element doesn't have any content (like br and img in HTML). Any does allow any content. NoElements will warn about elements from the same namespace (elements from other namespaces will be OK). NoElementsOrText will warn about elements from the same namespace and non-whitespace text content. Elements will only allow the elements specified in the constructor. ElementsOrText will only allow the elements specified in the constructor and text. None of these classes will check the number of child elements or their order. For more info see the sims module.