Java-Seekar Java-Seekar -4 years ago 217
Java Question

How to pass parameter to XSLT from java code

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.

Answer Source

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 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();

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download