GNOME Bugzilla – Bug 135303
Testing if parameters exist
Last modified: 2021-07-05 11:00:37 UTC
a way to test if a stylesheet parameter or variable is defined/exists without libxslt erroring out when trying to evaluate it? If stylesheet parameters are dynamically evaluated based on the input document (think dyn:evaluate(concat('$', $varname)) ), then being able to evaluate the existence of parameters without causing an error would be useful. It would provide a simple and concise mechanism for backwards compatibility of input documents with older stylesheets.
I'm afraid I don't quite understand what you are after. Could you please provide a sample stylesheet + document which demonstrates what is lacking, or where an error occurs? Thanks, Bill
Created attachment 24773 [details] Example stylesheet and input document
I added an example stylesheet and input document. When you initially run it, it will fail, complaining about being unable to evaluate the expression $param3. If you edit the input document and remove the reference to param3, the processing will complete.
It appears to me that your request is actually for an additional function built into libxslt in order to test whether or not an XPath expression is valid. If this is correct, I'm afraid this will not be possible, since it would make any stylesheet using it incompatible with other conforming processors. However, I really don't understand why you want to use the exslt "dynamic" function dyn:evaluate, rather than a simpler approach such as (modifying your 'gallery.xsl'): <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.1'> <xsl:output method="html" /> <xsl:param name="param1" select="'hello world'"/> <xsl:param name="param2" select="'goodbye world'"/> <xsl:template name="decodeParams"> <xsl:choose> <xsl:when test="@name='param1'"> <xsl:value-of select="$param1"/> </xsl:when> <xsl:when test="@name='param2'"> <xsl:value-of select="$param2"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="concat('Undefined parameter ', @name)"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="@*|node()"> <xsl:choose> <xsl:when test="name() = 'param'"> <xsl:call-template name="decodeParams"/> </xsl:when> <xsl:otherwise> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> which certainly works with all conforming xslt processors.
Yes, I understand it would be an extension. I can think of two reasons for such functionality being beneficial: 1. The solution you propose is obvious but, in my opinion, it violates the coding maxim of avoiding redundancy. Every time you add or remove a parameter you need to do so in two places. Yes, it is a small inconvenience but with many documents over long periods of time and many different coders, it reduces maintainability dramatically. 2. uniformity of functionality. You can find out if in an input document a particular node or particular element exists in a particular place. Why not be able to find out if a particular input parameter exists? It is not a huge feature, just a convenience. Thanks for considering it!
I'm still considering your request, and although I am still not agreeable to implementing some "special function" for this purpose within libxslt, I am willing to seriously consider an argument that, according to the exslt.org site, the dyn:evaluate function should have the characteristic that: "... If the expression string passed as the second argument is an invalid XPath expression (including an empty string), this function returns an empty node set." I guess we could argue whether or not a non-existent parameter is an "invalid XPath expression", but if you were to claim that it is, you would be on pretty strong ground :-). I'm not sure how hard it would be to enhance the coding of dyn:evaluate to allow (i.e. not error-out on) this condition, but I'm wondering whether this would be an acceptable alternative for you? Bill
I'm not familiar with the machinations of the exslt specification but as an alternative to creatively interpreting the definition of dyn:evaluate() would it not be possible to define another exslt function to perform the test? Otherwise, the your suggestion would work for me and sounds very defensible from a technical standpoint :)
GNOME is going to shut down bugzilla.gnome.org in favor of gitlab.gnome.org. As part of that, we are mass-closing older open tickets in bugzilla.gnome.org which have not seen updates for a longer time (resources are unfortunately quite limited so not every ticket can get handled). If you can still reproduce the situation described in this ticket in a recent and supported software version, then please follow https://wiki.gnome.org/GettingInTouch/BugReportingGuidelines and create a new ticket at https://gitlab.gnome.org/GNOME/libxslt/-/issues/ Thank you for your understanding and your help.