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