Posts Tagged ‘XSLT’

XSLT: xsl:include and XML namespace handling fixed in xsltproc

Posted in May 7, 201000:24hTim Strehle1 Comment »

You’re probably unlikely to run into the same problem, but just in case… I ran into an issue today where on one server, empty XML namespaces were inserted where they hadn’t been before. As I understand it, my sloppy XSLT programming has been tolerated by a bug in xsltproc (libxml2/libxslt) but that bug has been fixed.

Here’s my test.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<in/>

An XSLT file, include.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template name="external-element">
    <xsl:element name="p"/>
  </xsl:template>
</xsl:stylesheet>

And the main XSLT file, test.xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns="http://www.digicol.com/xmlns/dcx"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="xml" encoding="UTF-8"/>

  <xsl:include href="include.xslt"/>

  <xsl:template name="internal-element">
    <xsl:element name="p"/>
  </xsl:template>

  <xsl:template match="/in">
    <document>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <xsl:element name="p"/>
        <xsl:call-template name="internal-element"/>
        <xsl:call-template name="external-element"/>
      </body>
    </document>
  </xsl:template>

</xsl:stylesheet>

Our older xsltproc (“Using libxml 20626, libxslt 10117 and libexslt 813″) transformed this into:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://www.digicol.com/xmlns/dcx">
  <body xmlns="http://www.w3.org/1999/xhtml">
    <p/>
    <p xmlns="http://www.digicol.com/xmlns/dcx"/>
    <p/>
  </body>
</document>

A more current version of xsltproc (“Using libxml 20703, libxslt 10124 and libexslt 813″) and Saxon 6.5.5 both return what I suppose is correct:

<?xml version="1.0" encoding="UTF-8"?>
<document xmlns="http://www.digicol.com/xmlns/dcx">
  <body xmlns="http://www.w3.org/1999/xhtml">
    <p/>
    <p xmlns="http://www.digicol.com/xmlns/dcx"/>
    <p xmlns=""/>
  </body>
</document>

Interesting (if you’re into that kind of thing). I must admit that I hadn’t given namespaces much thought in this case.

(The fix is simple: Set the “namespace” attribute of xsl:element.)

TAGS: ,