root/nbdoc/trunk/notabene/xmlutils.py

Revision 854, 1.8 kB (checked in by rkern, 3 years ago)

Equation support for HTML.

  • Property svn:eol-style set to native
Line 
1 #*****************************************************************************
2 #       Copyright (C) 2005 Robert Kern. All rights reserved.
3 #
4 #  Distributed under the terms of the BSD License.  The full license is in
5 #  the file COPYING, distributed as part of this software.
6 #*****************************************************************************
7
8 class XMLNS(object):
9     """Convenience object for handling namespaced tag names in ElementTree-like
10     APIs.
11
12     ElementTree-like APIs use an inconvenient syntax for representing tags
13     within namespaces. E.g.
14
15         rdf:RDF -> {http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF
16         dc:creator -> {http://purl.org/dc/elements/1.1/}creator
17
18     Instances of XMLNS use __getattr__ magic to generate tags with this syntax.
19     You can also use __getitem__ for those tags which are not valid Python
20     identifiers.
21
22         rdf = XMLNS('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
23         xsl = XMLNS('xsl', 'http://www.w3.org/1999/XSL/Transform')
24         rdf.RDF == '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF'
25         xsl['value-of'] == '{http://www.w3.org/1999/XSL/Transform}value-of'
26
27     Constructor:
28         XMLNS(prefix, url)
29     """
30     def __init__(self, prefix, url):
31         self._url = url
32         self._qname_prefix = u'{%s}'%url
33         self._prefix = prefix
34
35     def __getitem__(self, tag):
36         return self._qname_prefix + tag
37     __getattr__ = __getitem__
38
39 def nsmap(*args):
40     """Create a dictionary mapping prefixes to namespace URLs from XMLNS
41     instances.
42     """
43     d = {}
44     for ns in args:
45         d[ns._prefix] = ns._url
46     return d
47    
48 rdf = XMLNS('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
49 dc = XMLNS('dc', 'http://purl.org/dc/elements/1.1/')
50 xsl = XMLNS('xsl', 'http://www.w3.org/1999/XSL/Transform')
51 xlink = XMLNS('xlink', 'http://www.w3.org/1999/xlink')
Note: See TracBrowser for help on using the browser.