How to pass a Document object as a parameter to XSLT

Posted on 2008-11-03
Last Modified: 2013-11-18
I am executing a XSLT from Java using this code ::

        DocumentBuilder builder;
        InputStream defInputStream = this.getClass().getClassLoader().getResourceAsStream("definitionsdb.xml");
        Document definitionsDB = builder.parse(defInputStream);

            Source xslSource = new StreamSource(inputXMLFile);
            Result xmlResult = new StreamResult(xmlOutputFileWriter);
            xmlTransformer.setParameter("inputFile", definitionsDB);
            xmlTransformer.transform(xslSource, xmlResult);

As you can see, apart from the StreamSource input xml file, I am sending another XML file as a parameter to the XSLT.

I receive the file in XSLT using the <xsl:param name="inputFile"/>

But then, I am not able to use the variable as a normal nodeset variable... like $inputFile/Definition/node1 ...

Any solution, on how to solve it?
Question by:emsenthil
    1 Comment
    LVL 27

    Accepted Solution

    XSLT (as a standard) only allows reference to document via file names, which it resolves and loads. I know of no method of passing the transformer a node-set in a document object. This of course makes it difficult to include XML or XSL source which one has in a string.

    The only suggestion I can make is to load an xml string containing two nodes containing the relevant parts :-

            <part id="1">....contents of the first file</part>
            <part id="2">....contents of the second file</part>
            .... and so on...

    At least with this method one doesn't need files to hold the data, it can come from strings.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now