我正在使用lxml python lib。將評估後的xpath傳遞給擴展
假設我們有產品XML如:
<product id='123' />
而且要應用XSL模板:
<xsl:template match="product">
<ssi:include virtual="/ssi/reviews/{@id}"/>
</xsl:template>
SSI:包括是一個簡單的lxml的擴展,插入nginx的SSI指令在HTML註釋碼。問題是評估@id並將屬性傳遞爲virtual =「/ ssi/include/123」。有沒有辦法? 我已經找到了解決辦法,並用它現在:
import lxml.etree
import re
from copy import deepcopy
ns = '{ssi}'
# ssi extensions
class SsiExtElement(lxml.etree.XSLTExtension):
def execute(self, context, self_node, input_node, output_parent):
_, tag = self_node.tag.split('}')
tmp = lxml.etree.Element('tmp')
for k, v in self_node.attrib.items():
if re.search('\{(.*)\}', v): #here we search {xpath} values to evaluate
elem = deepcopy(input_node)
matches = re.findall('\{(.*)\}', v)
for match in matches:
v = v.replace('{%s}' % match, elem.xpath(match)[0])
tmp.set(k, v)
self.process_children(context, output_parent=tmp)
attrs = ' '.join(u'%s="%s"' % (k, v) for k, v in tmp.attrib.items())
ssi = lxml.etree.Comment(u'#%s %s' % (tag, attrs))
output_parent.append(ssi)
for node in tmp:
output_parent.append(node)
if (self_node.tag.replace(ns,'') in ('if', 'else', 'elif')
and self_node.getnext().tag.replace(ns, '') not in ('else', 'elif')):
output_parent.append(lxml.etree.Comment(u'#endif'))
是的,thanx。因此解決方案有效,但並不像我需要實施的那麼簡潔。在這種情況下,我們不需要擴展,編寫自定義xsl:comment – 2013-04-29 14:10:05