Search code examples
xmlxsltsaxonxinclude

XInclude not working with Saxon?


I have this XSLT stylesheet xinclude-test.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xi="http://www.w3.org/2001/XInclude">
  <xsl:output method="text"/>
  <xi:include href="xinclude-test-included.xml"/>

  <!-- <xsl:param name="test"> -->
  <!--   aaa -->
  <!-- </xsl:param> -->

  <xsl:template name="main">
    <xsl:value-of select="$test"/>
  </xsl:template>
</xsl:stylesheet>

The included file xinclude-test-included.xml is

<?xml version="1.0" encoding="utf-8"?>
<xsl:param name="test" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
aaa
</xsl:param>

When I evaluate the stylesheet with

saxon -xi -xsl:xinclude-test.xsl -it:main -t

It prints

Saxon-HE 9.8.0.1J from Saxonica
Java version 1.8.0_31
Static error at char 5 in xsl:value-of/@select on line 13 column 35 of xinclude-test.xsl:
  XPST0008: Variable test has not been declared (or its declaration is not in scope)
Errors were reported during stylesheet compilation

where I expect it to print "aaa".

However if I use another stylesheet to load the first XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0">
  <xsl:template name="main">
    <xsl:copy-of select="document('xinclude-test.xsl')"/>
  </xsl:template>
</xsl:stylesheet>

it correctly does the XInclude processing.

So what did I do wrong in my first XSLT?


Solution

  • I'm sure you'll get an answer or comments about Saxon in particular, but as a more general answer to your question,

    So what did I do wrong in my first XSLT?

    I'll observe that XSLT 2.0 does not contain any provision requiring processors to perform XInclude processing on the stylesheet document. Inasmuch as the spec defines the form and semantics of stylesheet documents, I'm inclined to take that omission as an indication that processors should not perform XInclude processing on the stylesheet document. Whether the document() function performs XInclude processing is not specified either, but I don't take that as indicative in the same way.

    For better or for worse, XSLT has its own mechanism for including external stylesheet fragments. The semantics are a bit different from XInclude: xsl:include and xsl:import are used to bring in external stylesheet modules, not arbitrary XML fragments.