Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #19193
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <awilliam@whitemice.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'transform': 0.07; 'python': 0.08; "'''": 0.09; '425': 0.09; '[],': 0.09; 'ah,': 0.09; 'content-type:multipart/signed': 0.09; 'dict': 0.09; 'filename:fname piece:signature': 0.09; 'jose': 0.09; 'object?': 0.09; 'def': 0.13; 'extensions': 0.15; '","': 0.16; 'adam': 0.16; 'content-type:application/pgp-signature': 0.16; 'does,': 0.16; 'filename:fname piece:asc': 0.16; 'filename:fname:signature.asc': 0.16; 'from:addr:awilliam': 0.16; 'from:addr:whitemice.org': 0.16; 'from:name:adam tauno williams': 0.16; 'lookup': 0.16; 'marx': 0.16; 'overloaded': 0.16; 'received:72.14.190': 0.16; 'received:72.14.190.87': 0.16; 'received:mail.wmmi.net': 0.16; 'received:wmmi.net': 0.16; 'reply-to:addr:awilliam': 0.16; 'reply- to:addr:whitemice.org': 0.16; 'subject:lxml': 0.16; 'url:lxml': 0.16; 'williams,': 0.16; 'wrote:': 0.16; 'language': 0.17; 'developer,': 0.18; 'working.': 0.18; 'raymond': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; '+0100,': 0.23; 'dictionary': 0.23; 'wondered': 0.23; 'stefan': 0.24; 'code': 0.25; 'parameters': 0.25; 'sat,': 0.25; 'import': 0.27; "i'm": 0.27; 'pass': 0.28; 'example': 0.28; 'explicit': 0.28; "skip:' 10": 0.29; 'print': 0.29; 'seem': 0.29; 'received:72.14': 0.29; 'class': 0.29; "i've": 0.31; 'anyone': 0.31; 'source': 0.32; 'languages': 0.32; 'to:addr:python-list': 0.33; 'object': 0.33; 'eric': 0.34; 'subject:/': 0.34; 'function.': 0.34; 'test': 0.35; 'variables': 0.36; 'but': 0.37; 'using': 0.37; 'skip:_ 10': 0.37; 'should': 0.38; 'url:org': 0.39; 'plain': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'according': 0.60; '&': 0.61; 'your': 0.61; 'williams': 0.63; 'perfect': 0.64; 'here:': 0.67; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'cathedral': 0.84; 'karl': 0.84; 'manifesto': 0.84; 'ortega': 0.84; 'url:extensions': 0.84; 'xslt': 0.84; 'url:1999': 0.86; 'stupid': 0.91; 'story.': 0.95 |
| Subject | Re: etree/lxml/XSLT and dynamic stylesheet variables |
| From | Adam Tauno Williams <awilliam@whitemice.org> |
| To | python-list@python.org |
| Date | Sat, 21 Jan 2012 13:07:21 -0500 |
| In-Reply-To | <jfdgi0$p6a$1@dough.gmane.org> |
| References | <1327091891.5020.12.camel@linux-dauq.site> <jfdgi0$p6a$1@dough.gmane.org> |
| Content-Type | multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-BnJ+WPUFmssBtTDRVeeD" |
| X-Mailer | Evolution 3.2.1 |
| Mime-Version | 1.0 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| Reply-To | awilliam@whitemice.org |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4912.1327169245.27778.python-list@python.org> (permalink) |
| Lines | 130 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1327169245 news.xs4all.nl 6990 [2001:888:2000:d::a6]:48656 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:19193 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Sat, 2012-01-21 at 05:56 +0100, Stefan Behnel wrote:
> Adam Tauno Williams, 20.01.2012 21:38:
> > I'm using etree to perform XSLT transforms, such as -
> > from lxml import etree
> > source = etree.parse(self.rfile)
> > xslt = etree.fromstring(self._xslt)
> > transform = etree.XSLT(xslt)
> > result = transform(source)
> > according to the docs at
> > <http://lxml.de/xpathxslt.html#stylesheet-parameters> I can pass a
> > dictionary of parameters to transform, such as -
> > result = transform(doc_root, **{'non-python-identifier': '5'})
> > Can I pass a dictionary-like object? That doesn't seem to be working.
> Yes it does, Python copies it into a plain dict at call time.
Ah, I wondered if that was happening. In which case is supresses all
the magic of my dict subclass.
> > I need to perform dynamic lookup of variables for the stylesheet.
> Different story.
> > I've subclassed dictionary and overloaded [], get, has_key, and in to
> > perform the required lookups; these work in testing. But passing the
> > object to transform doesn't work
> You should make the lookup explicit in your XSLT code using an XPath
> function. See here:
> http://lxml.de/extensions.html
Perfect thanks; this provides everything I need.
A stupid test case / example for anyone interested:
from lxml import etree
class MyExt:
def __init__(self, languages):
self._languages = languages
def languagelookup(self, _, arg):
language = self._languages.get(arg)
if not language:
return 'undefined'
return language
extensions = etree.Extension( MyExt(languages={ 'ES': 'Spanish',
'EL': 'Greek',
'DE': 'German',
'EN': 'English' } ),
( 'languagelookup', ),
ns='847fe241-df88-45c6-b4a7' )
text = '''<documents>
<document>
<id>109</id>
<category>OP</category>
<title>Revolt Of The Masses</title>
<author>Jose Ortega y Gasset</author>
<published>1930</published>
<language translator="anonymous">ES</language>
</document>
<document>
<id>108</id>
<category>P</category>
<title>Meditations</title>
<author>Marcus Aurelius</author>
<language translator="Maxwell Staniforth">EL</language>
<published>1930</published>
</document>
<document>
<id>425</id>
<category>OP</category>
<title>The Communist Manifesto</title>
<author>Karl Marx</author>
<author>Friedrich Engels</author>
<language translator="Samuel Moore">DE</language>
<published>1914</published>
</document>
<document>
<id>507</id>
<category>POT</category>
<title>The Cathedral & The Bazaar</title>
<author>Eric S. Raymond</author>
<published>199</published>
</document>
</documents>'''
source = etree.fromstring(text)
style ='''<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="847fe241-df88-45c6-b4a7">
<xsl:output method="text"/>
<xsl:template match="/documents/document">
<xsl:value-of select="id"/>
<xsl:text>,"</xsl:text>
<xsl:value-of select="author"/>
<xsl:text>","</xsl:text>
<xsl:value-of select="ext:languagelookup(string(language))"/>
<xsl:text>"</xsl:text>
</xsl:template>
</xsl:stylesheet>'''
xslt = etree.XSLT(etree.XML(style), extensions=extensions)
print xslt(source)
--
Adam Tauno Williams <http://www.whitemiceconsulting.com>
System Administrator, OpenGroupware Developer, LPI / CNA
Fingerprint 8C08 209A FBE3 C41A DD2F A270 2D17 8FA4 D95E D383
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: etree/lxml/XSLT and dynamic stylesheet variables Adam Tauno Williams <awilliam@whitemice.org> - 2012-01-21 13:07 -0500
csiph-web