GNOME Bugzilla – Bug 559309
apply-templates in nested namespaces results in badly formed XML
Last modified: 2021-07-05 11:00:25 UTC
Please describe the problem: Please see the examples below. Steps to reproduce: With XML doc: <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xml" href="libxsltbug.xsl"?> <root> <elem1>elem1 text</elem1> <elem2>elem2 <elem22>elem22 text</elem22></elem2> <elem3> <elem4>elem4 text <elem44>elem44 text</elem44></elem4> </elem3> </root> and XSLT stylesheet: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://my.namespace/" version="1.0"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" media-type="application/xml"/> <xsl:template match="/"> <output> <xsl:apply-templates select="/root"/> </output> </xsl:template> <xsl:template match="/root"> <subout> <xsl:apply-templates select="child::*"/> </subout> </xsl:template> <xsl:template match="elem1"> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="elem2"> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:apply-templates select="child::elem22"/></div> </xsl:template> <xsl:template match="elem22"> <p xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="elem3"> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:apply-templates select="child::elem4"/></div> </xsl:template> <xsl:template match="elem4"> <p xmlns="http://www.w3.org/1999/xhtml"><xsl:apply-templates/></p> </xsl:template> <xsl:template match="elem44"> <code xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></code> </xsl:template> </xsl:stylesheet> Actual results: <?xml version="1.0" encoding="utf-8"?> <output xmlns="http://my.namespace/"> <subout> <div xmlns="http://www.w3.org/1999/xhtml">elem1 text</div> <div xmlns="http://www.w3.org/1999/xhtml"> <(null)_1:p xmlns="http://my.namespace/" xmlns:(null)_1="http://www.w3.org/1999/xhtml">elem22 text</(null)_1:p> </div> <div xmlns="http://www.w3.org/1999/xhtml"> <(null)_1:p xmlns="http://my.namespace/" xmlns:(null)_1="http://www.w3.org/1999/xhtml">elem4 text <code xmlns="http://www.w3.org/1999/xhtml">elem44 text</code></(null)_1:p> </div> </subout> </output> Expected results: <?xml version="1.0" encoding="utf-8"?> <output xmlns="http://my.namespace/"> <subout> <div xmlns="http://www.w3.org/1999/xhtml">elem1 text</div> <div xmlns="http://www.w3.org/1999/xhtml"> <p>elem22 text</p> </div> <div xmlns="http://www.w3.org/1999/xhtml"> <p>elem4 text <code>elem44 text</code></p> </div> </subout> </output> Does this happen every time? Yes Other information: I am running libxslt 1.1.24.
I found a work around by wrapping the apply-templates tags in variable tags and then using copy-of on the template variables: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://my.namespace/" version="1.0"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" media-type="application/xml"/> <xsl:template match="/"> <output> <xsl:apply-templates select="/root"/> </output> </xsl:template> <xsl:template match="/root"> <subout> <xsl:apply-templates select="child::*"/> </subout> </xsl:template> <xsl:template match="elem1"> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></div> </xsl:template> <xsl:template match="elem2"> <xsl:variable name="elemvar"><xsl:apply-templates select="elem22"/></xsl:variable> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:copy-of select="$elemvar"/></div> </xsl:template> <xsl:template match="elem22"> <p xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></p> </xsl:template> <xsl:template match="elem3"> <xsl:variable name="elemvar"><xsl:apply-templates select="elem4"/></xsl:variable> <div xmlns="http://www.w3.org/1999/xhtml"><xsl:copy-of select="$elemvar"/></div> </xsl:template> <xsl:template match="elem4"> <xsl:variable name="elemvar"><xsl:apply-templates/></xsl:variable> <p xmlns="http://www.w3.org/1999/xhtml"><xsl:copy-of select="$elemvar"/></p> </xsl:template> <xsl:template match="elem44"> <code xmlns="http://www.w3.org/1999/xhtml"><xsl:value-of select="."/></code> </xsl:template> </xsl:stylesheet>
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.