1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25
28
29 - def name(self, tag):
30 return "{%s}%s" % (self._namespace, tag)
31
32
34 """Initialize me with a DOM node or a DOM document node (the
35 toplevel node you get when parsing an XML file). Then use me
36 to generate fully qualified XML names.
37
38 >>> xml = '<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"></office>'
39 >>> from lxml import etree
40 >>> namer = XmlNamer(etree.fromstring(xml))
41 >>> namer.name('office', 'blah')
42 {urn:oasis:names:tc:opendocument:xmlns:office:1.0}blah
43 >>> namer.name('office:blah')
44 {urn:oasis:names:tc:opendocument:xmlns:office:1.0}blah
45
46 I can also give you XmlNamespace objects if you give me the abbreviated
47 namespace name. These are useful if you need to reference a namespace
48 continuously.
49
50 >>> office_ns = name.namespace('office')
51 >>> office_ns.name('foo')
52 {urn:oasis:names:tc:opendocument:xmlns:office:1.0}foo
53 """
54
56
57
58 if hasattr(dom_node, 'nsmap'):
59 self.nsmap = dom_node.nsmap
60 else:
61 self.nsmap = dom_node.getroot().nsmap
62
63 - def name(self, namespace_shortcut, tag=None):
64
65
66
67 if tag is None:
68 namespace_shortcut, tag = namespace_shortcut.split(':')
69 return "{%s}%s" % (self.nsmap[namespace_shortcut], tag)
70
73