[lxml-dev] String parameters to xslt transformation
lxml lacks ways to apply an external parameter containing both single and double quotes. The patch adds `transform` method to XSLT object with `params` and `strparams` argument. `params` works like `**kw` of `__call__` method (i.e. you still need to surround string literals with quotes). `strparams` are treated literally, so you do not need to use any escaping or quotes.
Hi, thanks for the patch. Alexander Shigin wrote:
lxml lacks ways to apply an external parameter containing both single and double quotes.
The patch adds `transform` method to XSLT object with `params` and `strparams` argument. `params` works like `**kw` of `__call__` method (i.e. you still need to surround string literals with quotes).
`strparams` are treated literally, so you do not need to use any escaping or quotes.
I thought about this a bit, and I dislike the idea of adding a new transform method only to support escaped parameters. I prefer having a function or method that does the escaping, so that you could do transform = etree.XSLT(...) result = transform(doc, string_var = transform.strparam("'hi'")) strparam() may return either an escaped string or a wrapper object that the transformation code special cases internally, not sure what's better. What do you think? Stefan
Hi, Stefan, Stefan Behnel wrote:
strparam() may return either an escaped string or a wrapper object that the transformation code special cases internally, not sure what's better.
What do you think?
I took another look at libxslt internals. There isn't any way to escape single or double quote in argument. A citation from libxslt/variables.c, xsltProcessUserParamInternal: * enclosed single quotes (double quotes). If the string which you want to * be treated literally contains both single and double quotes (e.g. Meet * at Joe's for "Twelfth Night" at 7 o'clock) then there is no suitable * quoting character. You cannot use ' or " inside the string * because the replacement of character entities with their equivalents is * done at a different stage of processing. The solution is to call * xsltQuoteUserParams or xsltQuoteOneUserParam. So, the only way is to create a special class like XSLTQuotedVariable. I've made a second version of the patch with `strparam()` interface.
Alexander Shigin wrote:
Stefan Behnel wrote:
strparam() may return either an escaped string or a wrapper object that the transformation code special cases internally, not sure what's better.
What do you think?
I took another look at libxslt internals. There isn't any way to escape single or double quote in argument. A citation from libxslt/variables.c, xsltProcessUserParamInternal:
* enclosed single quotes (double quotes). If the string which you want to * be treated literally contains both single and double quotes (e.g. Meet * at Joe's for "Twelfth Night" at 7 o'clock) then there is no suitable * quoting character. You cannot use ' or " inside the string * because the replacement of character entities with their equivalents is * done at a different stage of processing. The solution is to call * xsltQuoteUserParams or xsltQuoteOneUserParam.
So, the only way is to create a special class like XSLTQuotedVariable. I've made a second version of the patch with `strparam()` interface.
Thanks! That looks much better. I'll fix up a couple of things and check if I can't get Cython to support classmethods for this purpose. If all works out, I'll add it for 2.2. Stefan
В Птн, 06/02/2009 в 17:14 +0100, Stefan Behnel пишет:
Thanks! That looks much better. I'll fix up a couple of things and check if I can't get Cython to support classmethods for this purpose. If all works out, I'll add it for 2.2.
Oh, I totally forgot, but I think that ```XSLT.strparam``` is a bit strange for escaping method to be placed. It's only my point of view, but I prefer transform = etree.XSLT(...) result = transform(doc, string_var=etree.strparam("hi")) instead of result = transform(doc, string_var=transform.strparam("hi"))
Hi, Alexander Shigin wrote:
В Птн, 06/02/2009 в 17:14 +0100, Stefan Behnel пишет:
Thanks! That looks much better. I'll fix up a couple of things and check if I can't get Cython to support classmethods for this purpose. If all works out, I'll add it for 2.2.
Oh, I totally forgot, but I think that ```XSLT.strparam``` is a bit strange for escaping method to be placed. It's only my point of view, but I prefer transform = etree.XSLT(...) result = transform(doc, string_var=etree.strparam("hi")) instead of result = transform(doc, string_var=transform.strparam("hi"))
I'm not sure. My thoughts were that the only purpose of that function will be the use for XSLT string parameters, so you can either name it "xsltstrparam()" or "xslt_string_parameter()", which sounds lengthy and unwildy, or you can move it into a namespace that makes it clear what it does, i.e. "XSLT.strparam()". That way, you can easily reference it in a local variable if you prefer a function, but it keeps the functionality local to its intrinsic scope. Stefan
participants (2)
-
Alexander Shigin
-
Stefan Behnel