Search code examples
xslt-2.0saxonxslt-3.0

why does - result-document method="text" - export an xml declaration?


Sadly this question is wrong - i can't seem to replicate the behaviour, thanks to Martin Honnen below for forcing me to revisit my assumptions - I can't delete it.

Saxon 11.4

if I run this

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="3.0">
    <xsl:template match="/">
        <xsl:result-document method="text" encoding="utf-8">
            <xsl:text>foo</xsl:text>
        </xsl:result-document>
    </xsl:template>
</xsl:stylesheet>

i would expect a text document

foo

but i get this

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

I was trying to dynamically trying to select and output format based on a passed parameter, but obviously if i want a text output I don't want an xml declation in it.

P.S.

the input document can be any valid xml (unless I'm missing something) so

<foo/>

Solution

  • You haven't shown us what the input is nor explained how exactly you run the transformation and look at the result but for me the (currently using SaxonJ HE 11.5) XML workbench, with no input, and the code

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="3.0">
        <xsl:template name="xsl:initial-template">
            <xsl:result-document method="text" encoding="utf-8">
                <xsl:text>foo</xsl:text>
            </xsl:result-document>
        </xsl:template>
    </xsl:stylesheet>
    

    gives just

    foo
    

    as the sole and principal result.

    The same result if I use some input.

    I have also run SaxonJ 11 from the command line (using the current/latest 11.6) and it doesn't show any XML declaration, neither when directly writing to the console nor when outputting to a file:

    java -jar 'C:\Program Files\Saxonica\SaxonHE11-6J\saxon-he-11.6.jar' -s:.\sample1.xml -xsl:.\sheet1.xsl
    foo
    PS C:\Users\marti\OneDrive\Documents\xslt\blog-xslt-3-by-example\sole-xsl-result-doc-test> java -jar 'C:\Program Files\Saxonica\SaxonHE11-6J\saxon-he-11.6.jar' -s:.\sample1.xml -xsl:.\sheet1.xsl -o:result1.txt
    PS C:\Users\marti\OneDrive\Documents\xslt\blog-xslt-3-by-example\sole-xsl-result-doc-test> type .\result1.txt
    foo
    

    So it is not quite clear what you are doing or in what way (command line, some API, some editor) your are using Saxon and get the result you show in your question.