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: 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 To: python-list@python.org Date: Sat, 21 Jan 2012 13:07:21 -0500 In-Reply-To: References: <1327091891.5020.12.camel@linux-dauq.site> 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --=-BnJ+WPUFmssBtTDRVeeD Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D etree.parse(self.rfile) > > xslt =3D etree.fromstring(self._xslt) > > transform =3D etree.XSLT(xslt) > > result =3D transform(source) > > according to the docs at > > I can pass a > > dictionary of parameters to transform, such as - > > result =3D 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. =20 A stupid test case / example for anyone interested: from lxml import etree class MyExt: def __init__(self, languages): self._languages =3D languages def languagelookup(self, _, arg): language =3D self._languages.get(arg) if not language: return 'undefined' return language extensions =3D etree.Extension( MyExt(languages=3D{ 'ES': 'Spanish', 'EL': 'Greek',=20 'DE': 'German', 'EN': 'English' } ), ( 'languagelookup', ),=20 ns=3D'847fe241-df88-45c6-b4a7' ) text =3D ''' 109 OP Revolt Of The Masses Jose Ortega y Gasset 1930 ES 108 P Meditations Marcus Aurelius EL 1930 425 OP The Communist Manifesto Karl Marx Friedrich Engels DE 1914 507 POT The Cathedral & The Bazaar Eric S. Raymond 199 ''' source =3D etree.fromstring(text) style =3D''' ," "," " ''' xslt =3D etree.XSLT(etree.XML(style), extensions=3Dextensions) print xslt(source) --=20 Adam Tauno Williams System Administrator, OpenGroupware Developer, LPI / CNA Fingerprint 8C08 209A FBE3 C41A DD2F A270 2D17 8FA4 D95E D383 --=-BnJ+WPUFmssBtTDRVeeD Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.18 (GNU/Linux) iEYEABECAAYFAk8a/tkACgkQLRePpNle04Nq4wCfUqko0SDUICY40INfN7zKgC+0 M2YAn3oUUVEKgs1G/uDUpKQYWl0xI35p =JGZY -----END PGP SIGNATURE----- --=-BnJ+WPUFmssBtTDRVeeD--