0

I want to pass the parameter from java application for the indent attribute as below.

I can pass it from java code without any issue but defining the parameter in xslt is an issue. I did the sample below:

<xsl:param select="'yes'">

<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" indent="{$indent}" />

But when I use like above I am getting the error saying the way I defined the attribute indent is invalid. Please help me to resolve this issue.

Gab
  • 7,869
  • 4
  • 37
  • 68
Java-Seekar
  • 1,720
  • 5
  • 30
  • 52
  • See http://stackoverflow.com/questions/1667454/xsl-transformation-in-java-with-parameters - undermore `name="indent"` seems to be missing. For indentation you can do http://stackoverflow.com/questions/1384802/java-how-to-indent-xml-generated-by-transformer – Joop Eggen Jan 03 '14 at 16:02

1 Answers1

2

The declaration of the parameter with <xsl:param name="indent" select="'yes'"/> is correct but not all attributes of all elements allow an attribute value template. If we look at http://www.w3.org/TR/xslt20/#serialization then we see that those attributes don't allow an attribute value template as otherwise the syntax would say e.g. indent={yes|no}.

If you want to define the indentation in your Java code then check the API of your XSLT processor, it probably has a method to set output serialization settings.

Based on your comment, you are using IBM's Websphere XSLT 2.0 API, I don't have experience using that API so the following is an attempt to try to read the API online documentation to suggest a possible approach to serialize with your custom settings:

XOutputParameters params = yourXSLTExecutableInstance.getOutputParameters();
params.setIndent(true);

List<XItemView> result = yourXSLTExecutableInstance.executeToList(yourJAXPInputSource);
result.get(0).exportItem(yourJAXPStreamResult, params);

That's roughly what I would try, I don't have any access to the API to test.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • For `javax.xml.transform.Transformer` the API you're looking for is `setOutputProperty(OutputKeys.INDENT, "yes")` – Ian Roberts Jan 03 '14 at 16:55
  • Ian we are using IBM API for this transformation. We use XSLTExecutable.execute() for the transformation. We could not see any API to set the INDENT through the java program. If you have any idea please share with me. – Java-Seekar Jan 04 '14 at 10:03
  • @K.Senthuran, I have read through the online doc of the IBM API and tried to find a way to use custom output parameters, see my edited answer. Please note that the code is meant as a suggestion as to which interfaces and methods could help, I have no way of testing that so you will have to do that yourself and probably need to adapt the suggestion. – Martin Honnen Jan 04 '14 at 11:18
  • @ Martin, Your solution helped us to resolve the issue. It is working as expected. Thanks a lot. – Java-Seekar Jan 06 '14 at 13:22