Toolbox
Toolbox

Reputation: 2473

How to get info about which XSL version XSLT Processor runs

How to get info about which XSL version XSLT Processor runs?

I am evaluating Saxon Home Edition (HE10-SJ). Since this package seems to come with both XSL2.0 and XSL3.0 I am wondering how to get info about which XSL the XSLT Processor will run. I am fully aware that one sets the XSL version in the XSL file, but even if I add the XSL version in the XSL to 5 (that obviously doesn't exists), it will still run and process.

I also understand that if I would add code in XSL stylesheet that is exlusive to work with XSL2.0/XSL3.0, and set the XSL stylesheet to XSL1.0 I would most probably get either an error or the XSLT would not process the request.

I added the flag "-t" to get some information but only see java version, not XSLT/XSL version. Extraction of result is here:

Saxon-HE 10.5J from Saxonica
Java version 11.0.10
Using parser com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser

Upvotes: 0

Views: 1529

Answers (3)

Dave The Dane
Dave The Dane

Reputation: 869

Here's some code that seems to do the trick, but surely there has to be any easier way?

import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;

public final class StringSource extends StreamSource {

    final String string;

    private StringSource(final String string) {
        this.string = string;
    }
    @Override
    public Reader getReader() {
        return new StringReader(string);
    }

    private static final String STYLESHEET = ""
            + "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">"
            +     "<xsl:output method=\"xml\" omit-xml-declaration=\"yes\" />"
            +     "<xsl:template match=\"/\"><xsl:value-of select=\"system-property('xsl:version')\"/></xsl:template>"
            + "</xsl:stylesheet>";

    public static void main(final String[] args) throws Exception {

        final Templates    templates   = TransformerFactory.newInstance().newTemplates(new StringSource(STYLESHEET));

        final StringWriter writer      = new StringWriter();

        final Transformer  transformer = templates.newTransformer();
        ;                  transformer.transform(new StringSource("<AnyOldXml/>"), new StreamResult(writer));

        System.out.println("Max. Supported XSLT Version.: " + writer);
    }
}

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163262

An "XSLT processor" generally conforms to one specific version of the "XSLT specification". It's possible that a particular software product includes XSLT processors for multiple versions of the specification, but it's best to think of them as different processors even if they share code.

Given an XSLT processor and a stylesheet, the processor looks at the @version attribute and this affects how it behaves.

If @version="1.0", then an XSLT 2.0 or 3.0 processor will evaluate some constructs in "backwards compatibility mode", which is closer to the way that an XSLT 1.0 processor would have handled them. But it remains an XSLT 2.0 or 3.0 processor, following the rules of the 2.0 or 3.0 specification, and if the stylesheet uses constructs that require 2.0 or 3.0, these constructs will still work.

If version="5.0", say, the processor will work in "forwards compatibility mode", which basically means that it ignores constructs that it doesn't understand. The original intent of forwards compatibility mode was primarily for deployment on the web, to make it easier to write stylesheets that ran with any browser regardless which XSLT version it supported. It hasn't been very successful in achieving this aim, (a) because browsers have never moved beyond 1.0, and (b) because simply ignoring new constructs often leaves you with unusable results. This kind of concept makes much more sense for a language like CSS than it does for XSLT.

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167401

Saxon 9.8 and later are XSLT 3 processors, of course you can run XSLT 2 code or even XSLT 1.0 code with them, XSLT 1.0 in backwards compatible mode https://www.w3.org/TR/xslt-30/#backwards.

You can check system-property('xsl:version') for the XSLT version and also a property xsl:supports-backwards-compatibility if the backwards compatibility mode is supported; it is in Saxon 10, 9.9 and later 9.8 releases. I think only the earliest 9.8 HE releases tried to disable that mode.

Upvotes: 3

Related Questions