def startElement(self, name, attrs): prefix, local = SplitQName(name) if local == 'xmlns': (local, prefix) = (prefix, local) if prefix: self._out.write( ' <%s%s%s:%s%s%s'.encode(self._cod) % (self._o_d[_KEYWORD][0], prefix, self._o_d[_KEYWORD][1], self._o_d[_NAME][0], local, self._o_d[_NAME][1])) else: self._out.write(' <%s%s%s'.encode(self._cod) % (self._o_d[_NAME][0], local, self._o_d[_NAME][1])) for key, val in attrs.items(): prefix, local = SplitQName(key) if local == 'xmlns' and prefix: (local, prefix) = (prefix, local) if prefix: self._out.write( ' %s%s%s:%s%s%s=%s"%s"%s'.encode(self._cod) % (self._o_d[_KEYWORD][0], prefix, self._o_d[_KEYWORD][1], self._o_d[_NAME][0], local, self._o_d[_NAME][1], self._o_d[_STRING][0], val, self._o_d[_STRING][1])) else: self._out.write( ' %s%s%s=%s"%s"%s'.encode(self._cod) % (self._o_d[_NAME][0], local, self._o_d[_NAME][1], self._o_d[_STRING][0], val, self._o_d[_STRING][1])) self._out.write('>')
def startElement(self, name, attribs): self._completeTextNode() old_nss = {} del_nss = [] for curr_attrib_key, value in attribs.items(): (prefix, local) = SplitQName(curr_attrib_key) if local == 'xmlns': if self._namespaces.has_key(prefix): old_nss[prefix] = self._namespaces[prefix] if value: self._namespaces[prefix] = attribs[curr_attrib_key] else: del self._namespaces[prefix] elif value: self._namespaces[prefix] = attribs[curr_attrib_key] del_nss.append(prefix) self._namespaceStack.append((old_nss, del_nss)) (prefix, local) = SplitQName(name) nameSpace = self._namespaces.get(prefix, None) if self._ownerDoc: new_element = self._ownerDoc.createElementNS( nameSpace, (prefix and prefix + ':' + local) or local) else: self._initRootNode(nameSpace, name) new_element = self._ownerDoc.documentElement for curr_attrib_key, curr_attrib_value in attribs.items(): (prefix, local) = SplitQName(curr_attrib_key) qname = local if local == 'xmlns': namespace = XMLNS_NAMESPACE if prefix: qname = local + ':' + prefix attr = self._ownerDoc.createAttributeNS(namespace, qname) else: if prefix: qname = prefix + ':' + local namespace = prefix and self._namespaces.get(prefix, None) or None attr = self._ownerDoc.createAttributeNS(namespace, qname) attr.value = curr_attrib_value new_element.setAttributeNodeNS(attr) self._nodeStack.append(new_element) return
def endElement(self, name): prefix, local = SplitQName(name) if prefix: self._out.write('</%s%s%s:%s%s%s>'.encode(self._cod) % ( self._o_d[_KEYWORD][0], prefix, self._o_d[_KEYWORD][1], self._o_d[_NAME][0], local, self._o_d[_NAME][1])) else: self._out.write('</%s%s%s>'.encode(self._cod) % ( self._o_d[_NAME][0], local, self._o_d[_NAME][1]))
def attribute(self, name, value, namespace=EMPTY_NAMESPACE): prefix, localName = SplitQName(name) attr = self._ownerDoc.createAttributeNS(namespace, name) attr.value = value if self._nodeStack[-1].nodeType == Node.ELEMENT_NODE: self._nodeStack[-1].attributes[(namespace, localName)] = attr else: #Document-fragment parent self._nodeStack[-1].appendChild(attr) return
def ParsedFunctionCallExpr(name, args): name = string.strip(name) key = SplitQName(name) count = len(args) if count == 0: return FunctionCall(name, key, args) if count == 1: return FunctionCall1(name, key, args) if count == 2: return FunctionCall2(name, key, args) if count == 3: return FunctionCall3(name, key, args) return FunctionCallN(name, key, args)
def startElement(self, name, namespace=EMPTY_NAMESPACE, extraNss=None): extraNss = extraNss or {} prefix, localName = SplitQName(name) new_element = self._ownerDoc.createElementNS(namespace, name) self._nodeStack.append(new_element) for prefix in extraNss.keys(): if prefix: new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:' + prefix, extraNss[prefix]) else: new_element.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', extraNss[prefix]) new_element.stringValue = "" return
def findNamespaceURI(self, qualifiedName): parts = SplitQName(qualifiedName) element = self._getNode() if len(parts) == 1: return (self._dom.findTargetNS(element), value) return self._dom.findNamespaceURI(parts[0], element)
def startElement(self, name, attribs): self._completeTextNode() (name, qname, nsattribs) = self._handleStartElementNss(name, attribs) nsuri = name[0] local = name[1] prefix = SplitQName(qname)[0] mapping = g_mappings.get(nsuri, None) del_extu = [] if mapping: if not mapping.has_key(local): if self._firstElement: raise XsltException(Error.STYLESHEET_ILLEGAL_ROOT, name) else: raise XsltException(Error.XSLT_ILLEGAL_ELEMENT, local) xsl_class = mapping[local] if xsl_class == IncludeElement: #Can the included sheet have literal result element as root? inc = self.clone().fromUri(nsattribs[('', 'href')], baseUri=self._ssheetUri, ownerDoc=self._ownerDoc) sty = inc.firstChild included_nss = GetAllNs(sty) for child in sty.childNodes[:]: self._nodeStack[-1].appendChild(child) #migrate old nss from stylesheet directly to new child for prefix in included_nss.keys(): if prefix: child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns:' + prefix, included_nss[prefix]) else: child.setAttributeNS(XMLNS_NAMESPACE, 'xmlns', included_nss[prefix]) self._nodeStack.append(None) pDomlette.ReleaseNode(inc) return else: xsl_instance = xsl_class(self._ownerDoc, baseUri=self._ssheetUri) for aqname in nsattribs.getQNames(): (ansuri, alocal) = nsattribs.getNameByQName(aqname) value = nsattribs.getValueByQName(aqname) if ansuri != XMLNS_NAMESPACE and xsl_class == StylesheetElement: self._handleExtUris(ansuri, alocal, value, '', del_extu, xsl_instance) elif not ansuri and alocal not in xsl_instance.__class__.legalAttrs: raise XsltException(Error.XSLT_ILLEGAL_ATTR, aqname, xsl_instance.nodeName) xsl_instance.setAttributeNS(ansuri, aqname, value) else: if nsuri in self._extUris and self._extElements: #Default XsltElement behavior effects fallback ext_class = self._extElements.get((nsuri, local), XsltElement) xsl_instance = ext_class(self._ownerDoc, nsuri, local, prefix, self._ssheetUri) else: xsl_instance = LiteralElement(self._ownerDoc, nsuri, local, prefix, self._ssheetUri) for aqname in nsattribs.getQNames(): (ansuri, alocal) = nsattribs.getNameByQName(aqname) value = nsattribs.getValueByQName(aqname) if ansuri != XMLNS_NAMESPACE: self._handleExtUris(ansuri, alocal, value, '', del_extu, xsl_instance) if hasattr(xsl_instance.__class__, 'legalAttrs'): if not ansuri and alocal not in xsl_instance.__class__.legalAttrs: raise XsltException(Error.XSLT_ILLEGAL_ATTR, alocal, xsl_instance.nodeName) xsl_instance.setAttributeNS(ansuri, aqname, value) self._extUriStack.append(del_extu) if (xsl_instance.namespaceURI, xsl_instance.localName) == ( XSL_NAMESPACE, 'text') or xsl_instance.getAttributeNS( XML_NAMESPACE, 'space') == 'preserve': self._preserveStateStack.append(1) elif xsl_instance.getAttributeNS(XML_NAMESPACE, 'space') == 'default': self._preserveStateStack.append(0) else: self._preserveStateStack.append(self._preserveStateStack[-1]) if self._firstElement: self._initializeSheet(xsl_instance) self._nodeStack.append(xsl_instance) return
def __init__(self, name): self._name = name self._key = SplitQName(name[1:]) return