[lxml-dev] Defining name spaces in lxml/xslt

Hi, new to lxml I may be overlooking some hints in the doc files. I am trying to define a name space (myspace) in Python, but I don't know by which method my name space object is getting called from lxml/xslt. class AbcElement(ElementBase): def ###METHODNAME###(self): return 'Output of abc' ns = Namespace('http://xml.petr.com/xpyth3/ns/myspace') ns['abc'] = AbcElement xslt = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:abc="http://xml.petr.com/xpyth3/xpath/ns/myspace" xsl:extension-element-prefixes='myspace' > <xsl:template match="/"> <foo> < myspace:abc/> </foo> </xsl:template> </xsl:stylesheet>''' Where ###METHODNAME### I don't know. I am using lxml 1.0 beta Suggestions? Kind regards, Petr van Blokland ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------

Hi Petr, Petr van Blokland wrote:
"Implementing namespaces" is not quite what you think it is. It has nothing to do with XSLT. XSLT extension elements are not currently implemented in lxml (and require a rather heavy piece of code to be written). I assume you have read the documentation. http://codespeak.net/lxml/namespace_extensions.html It is mainly designed for writing custom APIs on top of XML. A good example for what you can do with it is the MathML implementation MathDOM. http://mathdom.sourceforge.net/ lxml will not "execute" the custom elements in any operation. (although it might be interesting to think about what you could do with this...) Stefan

Hi Stefan, Ah, that explains. I was searching for a feature that was not there. I'll have to think about another strategy then, This hase be working with a full-python XSL parser we wrote ourselves, but lxml/libxml2 is about 10 times faster, so it is very tempting to switch. I could solve the problem by only using xpath in combination with xsl:value-of. It might work (although not no readable as tags). Trying though I found that als < and > answered by an external xpath function are escaped. Any idea how to avoid that? Regards, Petr van Blokland ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------

Hi Petr, Petr van Blokland wrote:
Well, unfortunately, that's intentional. Maybe you could describe your use case in a little more detail so that I understand what you're trying to do. Is it only about injecting a tree into the XSL result? Or are you trying to make things configurable, i.e. do you need parametrized XSLT elements? Stefan

On Jun 2, 2006, at 9:41 PM, Stefan Behnel wrote:
Hi Stefan, I try to inject a tree (or even direct a source containing tags as return value from an external xpath function. I found a solution to it by using: <xsl:value-of select="xhtml:br()" disable-output-escaping="yes"/> where the br function is defined as: def xhtml_br(dummy): return '<br/>' and namespace: ns = FunctionNamespace('http://xml.petr.com/xpyth3/xpath/xhtml') ns.prefix = 'xhtml' ns['br'] = xhtml_br so this works. It fits my earlier need for creating external elements. Although a little less nice to read, this way it is possible to make the evaluation of tostring() from an XSLT transformation call the pieces of Python code during the process. We use this kind of technique to insert the result of specific functions as the result of SQL queries, etc. inside the XSL transformation. Some years ago we wrote our own XSL parser in Python that was almost but not quite following the standard. Now we are facing 3 disadvantages to that approach: 1 people using the system cannot use standard XSL knowledge and documentation 2 it is much slower than doing the processing in C 3 it allowed us to drift away from XSLT standard (which partly is an advandage, because it made features possible that are not available in the standard). Now I am trying to get the system run inside lxml because especially 1 and 2 look VERY promising. The part to solve is 3 where previous freedom needs to fit inside the "limitations" of lxml, libxml2 and real XSLT. But I am making progress. Thanks for the help until now. Petr ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------

Hi Petr, Petr van Blokland wrote:
Why don't you return an Element? You might have to use xsl:copy-of in that case, but it should do what you want. You should always try to stay within the world of XML with these things instead of writing out tags "by hand". Makes life easier. :)
It's not called in tostring(). It's called during the XSLT evaluation. You can traverse the result tree to see that what you generate above is really the string "<br/>", not an element. lxml just doesn't know what you meant to do, so it can't help you.
There is not that much you can do from extension elements that you could not do from XPath extensions. They are mainly syntactic sugar. Nice sugar, but sugar. Stefan

Hi Petr, Petr van Blokland wrote:
"Implementing namespaces" is not quite what you think it is. It has nothing to do with XSLT. XSLT extension elements are not currently implemented in lxml (and require a rather heavy piece of code to be written). I assume you have read the documentation. http://codespeak.net/lxml/namespace_extensions.html It is mainly designed for writing custom APIs on top of XML. A good example for what you can do with it is the MathML implementation MathDOM. http://mathdom.sourceforge.net/ lxml will not "execute" the custom elements in any operation. (although it might be interesting to think about what you could do with this...) Stefan

Hi Stefan, Ah, that explains. I was searching for a feature that was not there. I'll have to think about another strategy then, This hase be working with a full-python XSL parser we wrote ourselves, but lxml/libxml2 is about 10 times faster, so it is very tempting to switch. I could solve the problem by only using xpath in combination with xsl:value-of. It might work (although not no readable as tags). Trying though I found that als < and > answered by an external xpath function are escaped. Any idea how to avoid that? Regards, Petr van Blokland ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------

Hi Petr, Petr van Blokland wrote:
Well, unfortunately, that's intentional. Maybe you could describe your use case in a little more detail so that I understand what you're trying to do. Is it only about injecting a tree into the XSL result? Or are you trying to make things configurable, i.e. do you need parametrized XSLT elements? Stefan

On Jun 2, 2006, at 9:41 PM, Stefan Behnel wrote:
Hi Stefan, I try to inject a tree (or even direct a source containing tags as return value from an external xpath function. I found a solution to it by using: <xsl:value-of select="xhtml:br()" disable-output-escaping="yes"/> where the br function is defined as: def xhtml_br(dummy): return '<br/>' and namespace: ns = FunctionNamespace('http://xml.petr.com/xpyth3/xpath/xhtml') ns.prefix = 'xhtml' ns['br'] = xhtml_br so this works. It fits my earlier need for creating external elements. Although a little less nice to read, this way it is possible to make the evaluation of tostring() from an XSLT transformation call the pieces of Python code during the process. We use this kind of technique to insert the result of specific functions as the result of SQL queries, etc. inside the XSL transformation. Some years ago we wrote our own XSL parser in Python that was almost but not quite following the standard. Now we are facing 3 disadvantages to that approach: 1 people using the system cannot use standard XSL knowledge and documentation 2 it is much slower than doing the processing in C 3 it allowed us to drift away from XSLT standard (which partly is an advandage, because it made features possible that are not available in the standard). Now I am trying to get the system run inside lxml because especially 1 and 2 look VERY promising. The part to solve is 3 where previous freedom needs to fit inside the "limitations" of lxml, libxml2 and real XSLT. But I am making progress. Thanks for the help until now. Petr ---------------------------------------------- Petr van Blokland buro@petr.com | www.petr.com | +31 15 219 10 40 ----------------------------------------------

Hi Petr, Petr van Blokland wrote:
Why don't you return an Element? You might have to use xsl:copy-of in that case, but it should do what you want. You should always try to stay within the world of XML with these things instead of writing out tags "by hand". Makes life easier. :)
It's not called in tostring(). It's called during the XSLT evaluation. You can traverse the result tree to see that what you generate above is really the string "<br/>", not an element. lxml just doesn't know what you meant to do, so it can't help you.
There is not that much you can do from extension elements that you could not do from XPath extensions. They are mainly syntactic sugar. Nice sugar, but sugar. Stefan
participants (2)
-
Petr van Blokland
-
Stefan Behnel