United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-5064280 : XSLT output type XML does not honor {http://xml.apache.org/xslt}indent-amount in

Details
Type:
Bug
Submit Date:
2004-06-16
Status:
Resolved
Updated Date:
2012-04-25
Project Name:
JDK
Resolved Date:
2004-07-12
Component:
xml
OS:
linux_redhat_9.0
Sub-Component:
javax.xml.transform
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b58)

Related Reports
Backport:
Relates:

Sub Tasks

Description
It seems that Tiger builds do not honor the {http://xml.apache.org/xslt}indent-amount attribute on <{http://www.w3.org/1999/XSL/Transform}output> when using method="xml" indent="yes".

Consider the following program:

---%<---
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class Main {
    private static final String XSL =
        "<xsl:stylesheet version='1.0' " +
            "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " +
            "xmlns:xalan='http://xml.apache.org/xslt' " +
            "exclude-result-prefixes='xalan'>" +
        "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" +
        "<xsl:template match='/'>" +
        "<outer><inner/></outer>" +
        "</xsl:template>" +
        "</xsl:stylesheet>";
    private static final String XML = "<irrelevant/>";
    public static void main(String[] args) throws Exception {
        TransformerFactory fact = TransformerFactory.newInstance();
        Transformer t = fact.newTransformer(new StreamSource(new StringReader(XSL)));
        t.transform(new StreamSource(new StringReader(XML)), new StreamResult(System.out));
    }
}
---%<---

and its output under JDK 1.4.2_04 and JDK 1.5.0 b55:

---%<---
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b02)
Java HotSpot(TM) Client VM (build 1.4.2_04-b02, mixed mode)

<?xml version="1.0" encoding="UTF-8"?>
<outer>
    <inner/>
</outer>
---%<---

---%<---
java version "1.5.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b55)
Java HotSpot(TM) Client VM (build 1.5.0-beta3-b55, mixed mode, sharing)

<?xml version="1.0" encoding="UTF-8"?>
<outer>
<inner/>
</outer>
---%<---

The element <inner/> is no longer indented four spaces as was requested.

The attribute in question is of course not guaranteed by the JAXP or XSLT specifications, but it is permitted for the implementation to recognize it; and it is extremely useful when attempting to produce readable, structured XML output from XSLT, especially for long documents.

The attribute is supposed to be supported by the Xalan processor, according to

http://xml.apache.org/xalan-j/usagepatterns.html#outputprops

and as far as I am aware it is, when using Xalan as downloaded from xml.apache.org.

Perhaps just the XSLTC engine fails to support it and this is the reason for the functional regression in Tiger.

Whatever the cause, it seems that this should be very easy to fix; XML indentation is hardly a complex algorithm. I might even suggest turning on some nonzero indentation by default (only if the XSLT standard indent="yes" attribute is given, of course), since if you want line breaking you probably also want some indentation to make the output file more readable to a human.

For reference, the XSLT spec

http://www.w3.org/TR/xslt#section-XML-Output-Method

says

"If the indent attribute has the value yes, then the xml output method may output whitespace in addition to the whitespace in the result tree (possibly based on whitespace stripped from either the source document or the stylesheet) in order to indent the result nicely [...]"

The term "indent" is not defined, since the exact behavior is up to the implementation, but it is reasonable to assume that insertion of both line breaks and tab-like indentation within a line are desirable, since this is the style most often used for presentable XML.

If the user has switched to a different XSLT engine than is the default in JAXP, and it is not based on Xalan, of course this attribute should be quietly ignored as it is a hint.
###@###.### 2004-06-16
###@###.### 2004-06-22

                                    

Comments
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-rc

FIXED IN:
tiger-rc

INTEGRATED IN:
tiger-b58
tiger-rc


                                     
2004-09-19
EVALUATION

Committed the patch on main-branch of internal code-base. XSLTC  supports both the old namespace URI {http://xml.apache.org/xslt} and new namespace URI{http://xml.apache.org/xalan} for indent-amount.
###@###.### 2004-06-22
                                     
2004-06-22
SUGGESTED FIX

Restore behavior of JDK 1.4.2.
###@###.### 2004-06-16
                                     
2004-06-16
WORK AROUND

1. Use a custom copy of Xalan, or some other XSLT processor that does indentation, registering it to JAXP.

2. Use indent="no" and explicitly include all desired whitespace in the stylesheet and/or input document.

3. Send to a SAX or DOM source and pipe that into a pretty-printer.
###@###.### 2004-06-16
###@###.### 2004-06-22
                                     
2004-06-16



Hardware and Software, Engineered to Work Together