2

I have a piece of code, that converts an XML file and an XSLT file to HTML. I'm trying to test each case in the ErrorListener(fatal error, error and warning), but I can't find any documentation on what causes any of them.

I have tried making intentional errors in my documents, but i only get [Fatal Error].

The code I have, uses the same ErrorListener in TransfomerFactory and in Transformer.

private String convertXmlToHtml(Source xml, Source xslt) throws TransformerException {
    StringWriter sw = new StringWriter();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    ErrorListenerThrowOnFatal errorListener = new ErrorListenerThrowOnFatal();
    tFactory.setErrorListener(errorListener);

    Transformer transform = tFactory.newTransformer(xslt);
    transform.setErrorListener(errorListener);
    transform.transform(xml, new StreamResult(sw));

    return sw.toString();
}

What are specific scenarios when an error/warning is not fatal?

EDIT: The scenarios should be of errors/warnings in the input files. e.i. When will TransformerFactory.newTransformer() or Transformer.transform() create an error/warning.

1 Answer 1

4

The documentation of the ErrorListener is not particularly well aligned with the terminology of the XSLT 1.0 specification, and even less well aligned with later versions. That's partly because the designers of the JAXP API tried to abstract away from XSLT, with the idea that it could also be used with other transformation languages, but that never actually happened. So the interpretation is going to depend on the implementor of your particular XSLT processor.

I believe one processor will at least optionally send xsl:message output to the ErrorListener.warning() method, but that has always seems strange to me because xsl:message output is XML, not a string.

Another factor is that neither the ErrorListener design nor the XSLT 1.0 specification is careful to distinguish static errors from dynamic errors, and the treatment of the two really needs to be different. (Indeed, one of the weaknesses of the design is that there is no need for static errors in your stylesheet to be represented as Java exceptions.)

My interpretation is:

warning: a condition where you are not doing something wrong according to the spec, but where the processor wants to advise you that you might be doing something undesirable or unintended. An example might be navigating to the children of an attribute node (which can never do anything useful).

error: something that violates a rule in the spec, but where processing can continue (at least to the extent of looking for further errors). This includes pretty well all static (compile time) errors, and those dynamic errors (such as ambiguous template rule matches) that are explicitly drawn out as "recoverable errors" in the XSLT 1.0 spec. (The notion of "recoverable errors" has disappeared in later versions of the spec).

fatal error: anything, whether there's a rule in the spec or not, where execution cannot continue. Includes most dynamic errors, e.g. in 1.0 applying sum() to something that's not a node-set.

Sign up to request clarification or add additional context in comments.

1 Comment

I have found, that documentBuilderFactory.setValidating(true) before creating a DocumentBuilder to parse a file, will show errors, like Document root element "catalog", must match DOCTYPE root "null". I have yet to figure out how to show/cause a warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.