1

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?

MetroWind
  • 541
  • 4
  • 16
  • Consider adding `-t` to your Saxon call and then to show us exactly which version of Saxon you use. – Martin Honnen Jul 07 '17 at 20:29
  • @MartinHonnen Added version info. – MetroWind Jul 07 '17 at 20:53
  • The documentation of Saxon 9.8 in http://saxonica.com/html/documentation/using-xsl/commandline/ says "-xi:(on|off) Apply XInclude processing to all input XML documents (including schema and stylesheet modules as well as source documents)" so I would first suggest to try `-xi:on` instead of `-xi`. If that does not work then I guess you will have to wait what Michael Kay says. – Martin Honnen Jul 07 '17 at 21:27
  • I tried both `-xi:on` and `-xi`, same result. John Bollinger's answer brought me thinking that Saxon only does xinclude processing on the document being transformed, not on the stylesheet. – MetroWind Jul 07 '17 at 21:36

2 Answers2

3

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • I see. This is very helpful. Thanks! – MetroWind Jul 07 '17 at 20:36
  • The note at the start of section 3 discourages such an interpretation: "A stylesheet module is represented by an XDM element node... Although stylesheet modules will commonly be maintained in the form of documents conforming to XML 1.0 or XML 1.1, this specification does not mandate such a representation. As with source trees, the way in which stylesheet modules are constructed, from textual XML or otherwise, is outside the scope of this specification." – Michael Kay Jul 08 '17 at 22:19
  • (And IIRC, the potential use of XInclude to assemble the stylesheet module was one of the motivations behind this rule.) – Michael Kay Jul 09 '17 at 20:02
1

The documentation doesn't match the code, I'm afraid: Saxon makes no attempt to apply the -xi option to the stylesheet document. We will have to make a decision about whether to treat the code or the documentation as correct.

Raised as a bug here: https://saxonica.plan.io/issues/3343

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • In my answer, I opine that the omission of any mention of XInclude processing from the XSLT 2.0 recommendation should be taken as an indication that such processing should not be performed on stylesheet documents. After looking more closely, I find that XInclude elements appearing at the top level of a stylesheet should be considered [User-defined data elements](https://www.w3.org/TR/xslt20/#user-defined-top-level), and as I read the spec, such elements must not be used by a processor to produce a result tree different than would be produced if they were ignored. – John Bollinger Jul 08 '17 at 18:44