Value object that knows the semantics of an XML tag having a namespace prefix.
| 37 | |
| 38 | |
| 39 | class NamespacePrefixedTag(str): |
| 40 | """Value object that knows the semantics of an XML tag having a namespace prefix.""" |
| 41 | |
| 42 | def __new__(cls, nstag: str): |
| 43 | return super(NamespacePrefixedTag, cls).__new__(cls, nstag) |
| 44 | |
| 45 | def __init__(self, nstag: str): |
| 46 | self._pfx, self._local_part = nstag.split(":") |
| 47 | self._ns_uri = _nsmap[self._pfx] |
| 48 | |
| 49 | @classmethod |
| 50 | def from_clark_name(cls, clark_name: str) -> NamespacePrefixedTag: |
| 51 | nsuri, local_name = clark_name[1:].split("}") |
| 52 | nstag = "%s:%s" % (pfxmap[nsuri], local_name) |
| 53 | return cls(nstag) |
| 54 | |
| 55 | @property |
| 56 | def clark_name(self): |
| 57 | return "{%s}%s" % (self._ns_uri, self._local_part) |
| 58 | |
| 59 | @property |
| 60 | def local_part(self): |
| 61 | """ |
| 62 | Return the local part of the tag as a string. E.g. 'foobar' is |
| 63 | returned for tag 'f:foobar'. |
| 64 | """ |
| 65 | return self._local_part |
| 66 | |
| 67 | @property |
| 68 | def nsmap(self): |
| 69 | """ |
| 70 | Return a dict having a single member, mapping the namespace prefix of |
| 71 | this tag to it's namespace name (e.g. {'f': 'http://foo/bar'}). This |
| 72 | is handy for passing to xpath calls and other uses. |
| 73 | """ |
| 74 | return {self._pfx: self._ns_uri} |
| 75 | |
| 76 | @property |
| 77 | def nspfx(self): |
| 78 | """ |
| 79 | Return the string namespace prefix for the tag, e.g. 'f' is returned |
| 80 | for tag 'f:foobar'. |
| 81 | """ |
| 82 | return self._pfx |
| 83 | |
| 84 | @property |
| 85 | def nsuri(self): |
| 86 | """ |
| 87 | Return the namespace URI for the tag, e.g. 'http://foo/bar' would be |
| 88 | returned for tag 'f:foobar' if the 'f' prefix maps to |
| 89 | 'http://foo/bar' in _nsmap. |
| 90 | """ |
| 91 | return self._ns_uri |
| 92 | |
| 93 | |
| 94 | def namespaces(*prefixes: str): |
no outgoing calls
searching dependent graphs…