1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
########################################################################
#
# File Name: ApplyTemplatesElement.py
#
#
"""
Implementation of the XSLT Spec apply-templates stylesheet element.
WWW: http://4suite.com/4XSLT e-mail: support@4suite.com
Copyright (c) 1999-2000 FourThought Inc, USA. All Rights Reserved.
See http://4suite.com/COPYRIGHT for license and copyright information
"""
from xml.dom import EMPTY_NAMESPACE
import xml.dom.Element
import xml.dom.ext
import xml.xslt
from xml.xslt import XsltElement, XSL_NAMESPACE, XsltException, Error
from xml.xpath import XPathParser
from xml.xpath import Util, g_xpathRecognizedNodes
class ApplyTemplatesElement(XsltElement):
legalAttrs = ['select', 'mode']
def __init__(self, doc, uri=xml.xslt.XSL_NAMESPACE,
localName='apply-templates', prefix='xsl', baseUri=''):
XsltElement.__init__(self, doc, uri, localName, prefix, baseUri)
def setup(self):
self.__dict__['_nss'] = xml.dom.ext.GetAllNs(self)
mode_attr = self.getAttributeNS(EMPTY_NAMESPACE, 'mode')
if mode_attr == '':
self.__dict__['_mode'] = None
else:
split_name = Util.ExpandQName(
mode_attr,
namespaces=self._nss
)
self.__dict__['_mode'] = split_name
select = self.getAttributeNS(EMPTY_NAMESPACE, 'select')
if select:
parser = XPathParser.XPathParser()
self.__dict__['_expr'] = parser.parseExpression(select)
else:
self.__dict__['_expr'] = None
self.__dict__['_sortSpecs'] = []
self.__dict__['_params'] = []
for child in self.childNodes:
#All children should be sort and with-param
if child.namespaceURI == XSL_NAMESPACE:
if child.localName == 'sort':
self._sortSpecs.append(child)
elif child.localName == 'with-param':
self._params.append(child)
else:
raise XsltException(Error.ILLEGAL_APPLYTEMPLATE_CHILD)
else:
raise XsltException(Error.ILLEGAL_APPLYTEMPLATE_CHILD)
return
def instantiate(self, context, processor):
origState = context.copy()
context.setNamespaces(self._nss)
params = {}
mode = self._instantiateMode(context)
for param in self._params:
(name, value) = param.instantiate(context, processor)[1]
params[name] = value
if self._expr:
node_set = self._expr.evaluate(context)
else:
node_set = context.node.childNodes
size = len(node_set)
if size > 1 and len(self._sortSpecs):
node_set = self._sortSpecs[0].instantiate(context, processor, node_set, self._sortSpecs[1:])[1]
pos = 1
for node in node_set:
context.setNodePosSize((node,pos,size))
processor.applyTemplates(context, mode, params)
pos = pos + 1
context.set(origState)
return (context,)
def _instantiateMode(self,context):
return self._mode
def __getstate__(self):
base_state = XsltElement.__getstate__(self)
new_state = (base_state, self._nss, self._sortSpecs, self._params,self._expr,self._mode)
return new_state
def __setstate__(self, state):
XsltElement.__setstate__(self, state[0])
self._nss = state[1]
self._sortSpecs = state[2]
self._params = state[3]
self._expr = state[4]
self._mode = state[5]
return
|