Reputation: 51
I am currently using the following code to indent XML:
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
This indents the code perfectly well however, I am not sure what the http://xml.apache.org/xslt}indent-amount"
is doing. The URL is essential for the indentation. Could someone explain what this URL does and how it works?
Thank you! :)
Upvotes: 1
Views: 2328
Reputation: 1334
You are overriding default property indent-amount
which is defined in org.apache.xml.serializer
package. This enables indentation (since default is 0).
Output properties for XML, HTML, and text transformation output are defined in property files in the org.apache.xml.serializer package.
You can override the default value of these properties in your stylesheet by using the attributes of an xsl:output element. You can override the Xalan specific default settings as follows:
Declare the xalan namespace in your stylesheet element (xmlns:xalan="http://xml.apache.org/xalan").
Use the namespace prefix you assign (for example, "xalan") to redefine properties of interest in the stylesheet xsl:output element (for example, xalan:indent-amount="5"). The following stylesheet fragment declares the xalan namespace and sets indent-amount to 2:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan"> <xsl:output method="xml" encoding="UTF-8" indent="yes" xalan:indent-amount="2"/>
You can find more at http://xml.apache.org/xalan-j/usagepatterns.html under chapter Configuring serialization output properties
.
All this assuming your serializer is xalan specific
Upvotes: 2