def initImpl(self, element, stylesheet, options): self.name = (element.prefix, element.localName) self.ns = element.namespaceURI self.useSets = qnameListProperty(element, (XSLT_NAMESPACE, 'use-attribute-sets'), namespaces=options['namespaces'], default='', resolveDefault=False) options['excludeResultNS'].extend(properties.nsListProperty(element, (XSLT_NAMESPACE, 'exclude-result-prefixes'), namespaces=options['namespaces']) or []) options['extensionElementsNS'].extend(properties.nsListProperty(element, 'extension-element-prefixes', namespaces=options['namespaces']) or []) if (stringProperty(element, (XSLT_NAMESPACE, 'version'), default='1.0') != '1.0'): options['forwardsCompatible'] = True if (stringProperty(element, (XSLT_NAMESPACE, 'version')) == '1.0'): options['forwardsCompatible'] = False self.attrs = {} for i in range(element.attributes.length): attr = element.attributes.item(i) if attr.namespaceURI == XSLT_NAMESPACE: # do not copy xsl: attributes continue if attr.namespaceURI == xml.dom.XMLNS_NAMESPACE: # check if this namespace node is excluded localName = attr.localName if attr.localName != 'xmlns' else None nsUri = resolveNamespace(localName, options['namespaces'], resolveDefault=True) if nsUri in options['excludeResultNS']: continue self.attrs[(attr.prefix, attr.localName, attr.namespaceURI)] = xp.AttributeTemplate(attr.value) self.setContent(element.childNodes, stylesheet, options)
def initImpl(self, element, stylesheet, options): self.name = (element.prefix, element.localName) self.ns = element.namespaceURI self.useSets = qnameListProperty( element, (XSLT_NAMESPACE, 'use-attribute-sets'), namespaces=options['namespaces'], default='', resolveDefault=False) options['excludeResultNS'].extend( properties.nsListProperty( element, (XSLT_NAMESPACE, 'exclude-result-prefixes'), namespaces=options['namespaces']) or []) options['extensionElementsNS'].extend( properties.nsListProperty(element, 'extension-element-prefixes', namespaces=options['namespaces']) or []) if (stringProperty(element, (XSLT_NAMESPACE, 'version'), default='1.0') != '1.0'): options['forwardsCompatible'] = True if (stringProperty(element, (XSLT_NAMESPACE, 'version')) == '1.0'): options['forwardsCompatible'] = False self.attrs = {} for i in range(element.attributes.length): attr = element.attributes.item(i) if attr.namespaceURI == XSLT_NAMESPACE: # do not copy xsl: attributes continue if attr.namespaceURI == xml.dom.XMLNS_NAMESPACE: # check if this namespace node is excluded localName = attr.localName if attr.localName != 'xmlns' else None nsUri = resolveNamespace(localName, options['namespaces'], resolveDefault=True) if nsUri in options['excludeResultNS']: continue self.attrs[(attr.prefix, attr.localName, attr.namespaceURI)] = xp.AttributeTemplate(attr.value) self.setContent(element.childNodes, stylesheet, options)
def _parseStylesheetContent(self, doc, baseUri): sheet = doc.documentElement if sheet.namespaceURI != core.XSLT_NAMESPACE or not(sheet.localName == 'stylesheet' or sheet.localName == 'transform'): raise UnexpectedNode(doc.documentElement) # compile options (that are specified as xsl:stylesheet attributes) are valid only within this stylesheet # (not include and imports) [XSLT 7.1.1], [XSLT 14.1] options = {} options['forwardsCompatible'] = sheet.getAttribute('version') != '1.0' options['namespaces'] = getNamespaceBindings(sheet) options['extensionElementsNS'] = properties.nsListProperty(sheet, 'extension-element-prefixes', namespaces=options['namespaces']) or [] options['excludeResultNS'] = properties.nsListProperty(sheet, 'exclude-result-prefixes', namespaces=options['namespaces']) or [] options['baseUri'] = baseUri options['definedVars'] = [] stripSpace(sheet, preserveSpaceList=[(core.XSLT_NAMESPACE, 'text')]) for i in range(sheet.childNodes.length): node = sheet.childNodes.item(i) if node.nodeType != Node.ELEMENT_NODE: continue if node.namespaceURI != core.XSLT_NAMESPACE: # Ignore top-level elements with an unknown non-null namespace [XSLT 2.2] if node.namespaceURI == None: raise UnexpectedNode(doc.documentElement.tagName) continue if node.localName == 'import': # TODO: check that imports come first href = properties.stringProperty(node, 'href', required=True) self.imports.append(Stylesheet(href, dp=self.dp)) if node.localName == 'include': href = properties.stringProperty(node, 'href', required=True) self._parseStylesheetContent(self.dp.document(href, baseUri), self.dp.absUri(href, baseUri)) if node.localName == 'variable' or node.localName == 'param': varclass = elements.Param if node.localName == 'param' else elements.Variable var = varclass(node, self, options) self.variables[var.name] = var if node.localName == 'template': template = elements.Template(node, self, options) self.addTemplateRule(template) if node.localName == 'strip-space': space = elements.SpaceStripping(node) self.stripSpace.extend(space.nameTests()) if node.localName == 'preserve-space': space = elements.SpaceStripping(node) self.preserveSpace.extend(space.nameTests()) if node.localName == 'output': output = elements.Output(node, self, options) combineOutput(self.output, output.outputDict()) if node.localName == 'key': key = elements.Key(node, self, options) self.addKey(key) if node.localName == 'decimal-format': raise NotImplemented(node.localName) if node.localName == 'namespace-alias': alias = elements.NamespaceAlias(node, self, options) ss, res = alias.getTuple() # accept one that occurs last self.namespaceAliases[ss] = res if node.localName == 'attribute-set': aset = AttributeSet(node, self, options) if aset.name in self.attrSets: self.attrSets[aset.name].update(aset) else: self.attrSets[aset.name] = aset
def _parseStylesheetContent(self, doc, baseUri): sheet = doc.documentElement if sheet.namespaceURI != core.XSLT_NAMESPACE or not ( sheet.localName == 'stylesheet' or sheet.localName == 'transform'): raise UnexpectedNode(doc.documentElement) # compile options (that are specified as xsl:stylesheet attributes) are valid only within this stylesheet # (not include and imports) [XSLT 7.1.1], [XSLT 14.1] options = {} options['forwardsCompatible'] = sheet.getAttribute('version') != '1.0' options['namespaces'] = getNamespaceBindings(sheet) options['extensionElementsNS'] = properties.nsListProperty( sheet, 'extension-element-prefixes', namespaces=options['namespaces']) or [] options['excludeResultNS'] = properties.nsListProperty( sheet, 'exclude-result-prefixes', namespaces=options['namespaces']) or [] options['baseUri'] = baseUri options['definedVars'] = [] stripSpace(sheet, preserveSpaceList=[(core.XSLT_NAMESPACE, 'text')]) for i in range(sheet.childNodes.length): node = sheet.childNodes.item(i) if node.nodeType != Node.ELEMENT_NODE: continue if node.namespaceURI != core.XSLT_NAMESPACE: # Ignore top-level elements with an unknown non-null namespace [XSLT 2.2] if node.namespaceURI == None: raise UnexpectedNode(doc.documentElement.tagName) continue if node.localName == 'import': # TODO: check that imports come first href = properties.stringProperty(node, 'href', required=True) self.imports.append(Stylesheet(href, dp=self.dp)) if node.localName == 'include': href = properties.stringProperty(node, 'href', required=True) self._parseStylesheetContent(self.dp.document(href, baseUri), self.dp.absUri(href, baseUri)) if node.localName == 'variable' or node.localName == 'param': varclass = elements.Param if node.localName == 'param' else elements.Variable var = varclass(node, self, options) self.variables[var.name] = var if node.localName == 'template': template = elements.Template(node, self, options) self.addTemplateRule(template) if node.localName == 'strip-space': space = elements.SpaceStripping(node) self.stripSpace.extend(space.nameTests()) if node.localName == 'preserve-space': space = elements.SpaceStripping(node) self.preserveSpace.extend(space.nameTests()) if node.localName == 'output': output = elements.Output(node, self, options) combineOutput(self.output, output.outputDict()) if node.localName == 'key': key = elements.Key(node, self, options) self.addKey(key) if node.localName == 'decimal-format': raise NotImplemented(node.localName) if node.localName == 'namespace-alias': alias = elements.NamespaceAlias(node, self, options) ss, res = alias.getTuple() # accept one that occurs last self.namespaceAliases[ss] = res if node.localName == 'attribute-set': aset = AttributeSet(node, self, options) if aset.name in self.attrSets: self.attrSets[aset.name].update(aset) else: self.attrSets[aset.name] = aset