|
8 | 8 |
|
9 | 9 |
|
10 | 10 | nsmap = { |
11 | | - 'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main', |
| 11 | + 'w': ('http://schemas.openxmlformats.org/wordprocessingml/2006/main'), |
| 12 | + 'wp': ('http://schemas.openxmlformats.org/drawingml/2006/wordprocessingD' |
| 13 | + 'rawing'), |
12 | 14 | } |
13 | 15 |
|
14 | 16 | # configure XML parser |
|
25 | 27 | # return oxml_parser.makeelement(qn(tag), nsmap=nsmap) |
26 | 28 |
|
27 | 29 |
|
| 30 | +class NamespacePrefixedTag(str): |
| 31 | + """ |
| 32 | + Value object that knows the semantics of an XML tag having a namespace |
| 33 | + prefix. |
| 34 | + """ |
| 35 | + def __new__(cls, nstag, *args): |
| 36 | + return super(NamespacePrefixedTag, cls).__new__(cls, nstag) |
| 37 | + |
| 38 | + def __init__(self, nstag): |
| 39 | + self._pfx, self._local_part = nstag.split(':') |
| 40 | + self._ns_uri = nsmap[self._pfx] |
| 41 | + |
| 42 | + @property |
| 43 | + def clark_name(self): |
| 44 | + return '{%s}%s' % (self._ns_uri, self._local_part) |
| 45 | + |
| 46 | + @property |
| 47 | + def local_part(self): |
| 48 | + """ |
| 49 | + Return the local part of the tag as a string. E.g. 'foobar' is |
| 50 | + returned for tag 'f:foobar'. |
| 51 | + """ |
| 52 | + return self._local_part |
| 53 | + |
| 54 | + @property |
| 55 | + def nsmap(self): |
| 56 | + """ |
| 57 | + Return a dict having a single member, mapping the namespace prefix of |
| 58 | + this tag to it's namespace name (e.g. {'f': 'http://foo/bar'}). This |
| 59 | + is handy for passing to xpath calls and other uses. |
| 60 | + """ |
| 61 | + return {self._pfx: self._ns_uri} |
| 62 | + |
| 63 | + @property |
| 64 | + def nspfx(self): |
| 65 | + """ |
| 66 | + Return the string namespace prefix for the tag, e.g. 'f' is returned |
| 67 | + for tag 'f:foobar'. |
| 68 | + """ |
| 69 | + return self._pfx |
| 70 | + |
| 71 | + @property |
| 72 | + def nsuri(self): |
| 73 | + """ |
| 74 | + Return the namespace URI for the tag, e.g. 'http://foo/bar' would be |
| 75 | + returned for tag 'f:foobar' if the 'f' prefix maps to |
| 76 | + 'http://foo/bar' in nsmap. |
| 77 | + """ |
| 78 | + return self._ns_uri |
| 79 | + |
| 80 | + |
28 | 81 | def nsdecls(*prefixes): |
29 | 82 | return ' '.join(['xmlns:%s="%s"' % (pfx, nsmap[pfx]) for pfx in prefixes]) |
30 | 83 |
|
31 | 84 |
|
32 | | -def OxmlElement(tag, attrs=None): |
33 | | - return oxml_parser.makeelement(qn(tag), attrib=attrs, nsmap=nsmap) |
| 85 | +def OxmlElement(nsptag_str, attrs=None, nsmap=None): |
| 86 | + """ |
| 87 | + Return a 'loose' lxml element having the tag specified by *nsptag_str*. |
| 88 | + *nsptag_str* must contain the standard namespace prefix, e.g. 'a:tbl'. |
| 89 | + The resulting element is an instance of the custom element class for this |
| 90 | + tag name if one is defined. A dictionary of attribute values may be |
| 91 | + provided as *attrs*; they are set if present. |
| 92 | + """ |
| 93 | + nsptag = NamespacePrefixedTag(nsptag_str) |
| 94 | + nsmap = nsmap if nsmap is not None else nsptag.nsmap |
| 95 | + return oxml_parser.makeelement( |
| 96 | + nsptag.clark_name, attrib=attrs, nsmap=nsmap |
| 97 | + ) |
34 | 98 |
|
35 | 99 |
|
36 | 100 | def oxml_fromstring(text): |
|
0 commit comments