我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用xml.sax.xmlreader.AttributesNSImpl()。
def test_nsattrs_wattr(self): attrs = AttributesNSImpl({(ns_uri, "attr") : "val"}, {(ns_uri, "attr") : "ns:attr"}) self.assertEqual(attrs.getLength(), 1) self.assertEqual(attrs.getNames(), [(ns_uri, "attr")]) self.assertEqual(attrs.getQNames(), ["ns:attr"]) self.assertEqual(len(attrs), 1) self.assertIn((ns_uri, "attr"), attrs) self.assertEqual(list(attrs.keys()), [(ns_uri, "attr")]) self.assertEqual(attrs.get((ns_uri, "attr")), "val") self.assertEqual(attrs.get((ns_uri, "attr"), 25), "val") self.assertEqual(list(attrs.items()), [((ns_uri, "attr"), "val")]) self.assertEqual(list(attrs.values()), ["val"]) self.assertEqual(attrs.getValue((ns_uri, "attr")), "val") self.assertEqual(attrs.getValueByQName("ns:attr"), "val") self.assertEqual(attrs.getNameByQName("ns:attr"), (ns_uri, "attr")) self.assertEqual(attrs[(ns_uri, "attr")], "val") self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr")
def write_header(self, allvarsL): self.writer.startElementNS( (SPARQL_XML_NAMESPACE, u'head'), u'head', AttributesNSImpl({}, {})) for i in xrange(0, len(allvarsL)): attr_vals = { (None, u'name'): unicode(allvarsL[i]), } attr_qnames = { (None, u'name'): u'name', } self.writer.startElementNS( (SPARQL_XML_NAMESPACE, u'variable'), u'variable', AttributesNSImpl(attr_vals, attr_qnames)) self.writer.endElementNS((SPARQL_XML_NAMESPACE, u'variable'), u'variable') self.writer.endElementNS((SPARQL_XML_NAMESPACE, u'head'), u'head')
def to_sax(walker, handler): """Call SAX-like content handler based on treewalker walker""" handler.startDocument() for prefix, namespace in prefix_mapping.items(): handler.startPrefixMapping(prefix, namespace) for token in walker: type = token["type"] if type == "Doctype": continue elif type in ("StartTag", "EmptyTag"): attrs = AttributesNSImpl(token["data"], unadjustForeignAttributes) handler.startElementNS((token["namespace"], token["name"]), token["name"], attrs) if type == "EmptyTag": handler.endElementNS((token["namespace"], token["name"]), token["name"]) elif type == "EndTag": handler.endElementNS((token["namespace"], token["name"]), token["name"]) elif type in ("Characters", "SpaceCharacters"): handler.characters(token["data"]) elif type == "Comment": pass else: assert False, "Unknown token type" for prefix, namespace in prefix_mapping.items(): handler.endPrefixMapping(prefix) handler.endDocument()
def start_element_ns(self, name, attrs): pair = name.split() if len(pair) == 1: # no namespace pair = (None, name) elif len(pair) == 3: pair = pair[0], pair[1] else: # default namespace pair = tuple(pair) newattrs = {} qnames = {} for (aname, value) in attrs.items(): parts = aname.split() length = len(parts) if length == 1: # no namespace qname = aname apair = (None, aname) elif length == 3: qname = "%s:%s" % (parts[2], parts[1]) apair = parts[0], parts[1] else: # default namespace qname = parts[1] apair = tuple(parts) newattrs[apair] = value qnames[apair] = qname self._cont_handler.startElementNS(pair, None, AttributesNSImpl(newattrs, qnames))
def __init__(self, element_or_tree, content_handler): try: element = element_or_tree.getroot() except AttributeError: element = element_or_tree self._element = element self._content_handler = content_handler from xml.sax.xmlreader import AttributesNSImpl as attr_class self._attr_class = attr_class self._empty_attributes = attr_class({}, {})
def test_nsattrs_empty(self): self.verify_empty_nsattrs(AttributesNSImpl({}, {}))