Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1007227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:35:09+00:00 2026-05-16T08:35:09+00:00

The top answer to this question describes a technique to implement an efficient XSLT

  • 0

The top answer to this question describes a technique to implement an efficient XSLT pipeline in Java:

Efficient XSLT pipeline in Java (or redirecting Results to Sources)

Unfortunately, while Transformer seems to expose an API for setting XSLT parameters, this does not seem to have any effect. For example, I have the following code:

Transformer.java

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.sax.TransformerHandler; 
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Transformer;
import java.io.File;
public class MyTransformer {
    public static void main(String[] args) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException{
        SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();

        // These templates objects could be reused and obtained from elsewhere.
        Templates templates1 = stf.newTemplates(new StreamSource( new File("MyStylesheet1.xslt")));
        Templates templates2 = stf.newTemplates(new StreamSource(new File("MyStylesheet2.xslt")));

        TransformerHandler th1 = stf.newTransformerHandler(templates1);
        TransformerHandler th2 = stf.newTransformerHandler(templates2);

        th1.setResult(new SAXResult(th2));
        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

            //SETTING PARAMETERS HERE
        t.setParameter("foo","this is from param 1");
        t.setParameter("bar","this is from param 2");

        t.transform(new StreamSource(new File("in.xml")), new SAXResult(th1));

        // th1 feeds th2, which in turn feeds System.out.
    }
}

MyStylesheet1.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform"  xmlns:foo="urn:foo" version="1.0">
    <output method="xml"/>

    <param name="foo"/>

    <template match="@*|node()">
        <copy>
            <apply-templates select="@*|node()"/>
        </copy>
    </template>

    <template match="foo:my/foo:hello">
        <copy>
            <foo:world>
                foo is : <value-of select="$foo"/>
            </foo:world>
        </copy>

    </template>
</stylesheet>

MyStylesheet2.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:foo="urn:foo" version="1.0">
    <output method="xml"/>

    <param name="bar"/>

    <template match="@*|node()">
        <copy>
            <apply-templates select="@*|node()"/>
        </copy>
    </template>

    <template match="foo:my/foo:hello/foo:world">
        <copy>
            <apply-templates select="@*|node()"/>

            <attribute name="attr">
                <value-of select="$bar"/>
            </attribute>
        </copy>

    </template>
</stylesheet>

in.xml

<my xmlns="urn:foo">
    <hello/>
</my>

Which gives me the following output:

<?xml version="1.0" encoding="UTF-8"?><my xmlns="urn:foo">
        <hello><foo:world xmlns:foo="urn:foo">foo is : </foo:world></hello>
</my>

As you can see foo:world/@attr is empty, and the text contents of foo:world says “foo is:”. The expected behaviour is that they should have been populated with the parameters passed into the setParameter method.

Is there a way to set XSL transformation parameters using this technique. If not, could anyone recommend an alternative technique for transforming stylesheets efficiently in Java, such that XSLT parameters may also be set?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:35:09+00:00Added an answer on May 16, 2026 at 8:35 am

    The problem is that each TransformerHandler has a separate Transformer associated with it. There’s a problem with your 2nd template, but as this is an example I guess that doesn’t matter. You want:

    //SETTING PARAMETERS HERE
    th1.getTransformer().setParameter("foo","this is from param 1");
    th2.getTransformer().setParameter("bar","this is from param 2");
    

    Note that you also don’t need to create a 3rd transformer you can just start the transform chain on th1.getTransformer()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The top answer to this question tells me how to stop/start a remote service.
See the top answer to this question: What exactly is Spring Framework for? Im
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
The top voted answer to this SA question ( Objective C Static Class Level
In Hidden Features of Java the top answer mentions Double Brace Initialization , with
There may be a simple answer to this question, but for the life of
I was trying to answer this question. As suggested by the accepted answer, the
I have touched on this question here , where Christopher gave an answer to
Reading what is currently the top answer to a recent question on how/whether to
I know this question has been asked many times and the answer is always

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.