mowglim
mowglim

Reputation: 1

Saxon is throwing "Cannot use xsl:result-document to write to a destination already used for the principal output:"

This is my first question here so please don't beat me up to hard :) I am using Apache Camel with Saxon XSLT transformer. And my goal is to output the results of an XSLT transformation into a file. I searched a lot but could not find any hints as to why I am getting that error.

This is my XSLT snippet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:xml="http://www.w3.org/XML/1998/namespace"
            xmlns:core="http://interoperability.gc.ca/core/1.0"
            exclude-result-prefixes="xs xsi xsl">
<xsl:output method="text" encoding="utf-8" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
    <xsl:variable name="someE">
        <someE>
            <someA>aaaa</someA>
            <someB>bbbb</someB>
        </someE>
    </xsl:variable>
    <xsl:result-document href="transformer.out" method="text" omit-xml-declaration="yes" encoding="utf-8"/>
        <xsl:copy-of select="$someE"/>
    <xsl:result-document/>
</xsl:template>
</xsl:stylesheet>

where $destinationAbsolutePath is the absolute file path (file:///C:/Temp/output.txt) of the output file.

What am I missing ?

Update: I ran the transformation from the command line and got the same failure:

C:\Temp\osfsa>java -jar Saxon-HE-9.9.1-7.jar -t -o:C:\Temp\osfsa\out\index.out -s:C:\Temp\osfsa\in\exporter.out -xsl:C:\Temp\osfsa\xml2fixedlength.xsl
Saxon-HE 9.9.1.7J from Saxonica
Java version 11.0.13
Stylesheet compilation time: 1.1386491s (1138.6491ms)
Processing file:/C:/Temp/osfsa/in/exporter.out
Using parser com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser
Building tree for file:/C:/Temp/osfsa/in/exporter.out using class net.sf.saxon.tree.tiny.TinyBuilder
Tree built in 12.0011ms
Tree size: 62 nodes, 157 characters, 9 attributes
Writing to file:/C:/Temp/osfsa/out/transformer.out
Error in xsl:result-document/@href on line 18 column 112 of xml2fixedlength.xsl:
XTDE1490: Cannot use xsl:result-document to write to a destination already used for the
principal output: file:/C:/Temp/osfsa/xml2fixedlength.xsl
In template rule with match="/" on line 11 of xml2fixedlength.xsl
Cannot use xsl:result-document to write to a destination already used for the principal output: file:/C:/Temp/osfsa/xml2fixedlength.xsl

Upvotes: 0

Views: 528

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

You've obviously simplified your code for demonstration purposes (we can tell because it's not well-formed, and because the error message refers to line 27), which is fine in principle, except that I think you've removed something critical to the error.

Here's an example that will give you the error. If you run a transformation with file:///out.xml as the principal output destination (specified when the transformation is invoked), and the transformation then does:

<xsl:template match="/">
  xxx
  <xsl:result-document href="file:///out.xml">
     yyy
  </xsl:result-document>
</xsl:template>

then you will get this error. The key ingredients are

(a) the href value on xsl:result-document is the same URI (after absolutisation) as the principal output URI

(b) some output has already been written ('xxx' in this example) when xsl:result-document is evaluated.

I hope this helps you resolve it. If not, you need to give us more details of exactly what you are running and how.

Update

There's something a little bit strange here, which is that the URI appearing in the error message is the URI of the stylesheet; I would expect it to be the output URI. That might just be poor diagnostics, I will check.

Further Update

Your code contains two xsl:result-document instructions, both empty:

<xsl:result-document href="transformer.out" method="text" omit-xml-declaration="yes" encoding="utf-8"/>
<xsl:copy-of select="$someE"/>
<xsl:result-document/>

You presumably intended to write a single instruction, with xsl:copy-of as its content:

<xsl:result-document href="transformer.out" method="text" omit-xml-declaration="yes" encoding="utf-8">
   <xsl:copy-of select="$someE"/>
</xsl:result-document>

The second xsl:result-document instruction has no @href attribute, so it defaults to writing to the principal output; but the xsl:copy-of instruction has already written to the principal output.

Now I need to understand why the wrong URI appears in the error message.

Upvotes: 2

Related Questions