[xsl] Xalan setParmeter

Subject: [xsl] Xalan setParmeter
From: "Fraser Goffin" <goffinf@xxxxxxxxxxx>
Date: Mon, 07 Nov 2005 23:33:11 +0000
OK, I found postings on this problem going back to 2001, but unfortunately not the answer that helps me, so if you've been here before, please bear with me.

The problem I'm trying to solve is how to pass a parameter into my XSLT processor (Xalan in this case) that is a reference to a DOM node (fragment), and then use that parameter within the XSLT.

Below is the XML instance source for the parameter, the XSLT (just a simple copy-of) and a small Java code example that sets the parameter in the parser, does the transform and outputs the result to the console. It all works to a point but the XSLT output is not quite what I expected. This is what the output looks like :-

<?xml version="1.0" encoding="UTF-8"?>
<rootContainer>org.dom4j.tree.DefaultElement@20dfa273 [Element: &lt;Odds attributes: []/&gt;]</rootContainer>

I was expecting :-

<?xml version="1.0" encoding="UTF-8"?>

If I use a simple value for the parameter, that is a string rather than a node, it works fine ??

Can anyone see what I'm doing wrong ??



Source XML Instance for the param

<?xml version="1.0" encoding="UTF-8"?>


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:param name="inboundXML" select="/" />
<xsl:template match="/">
<!--<xsl:value-of select="$inboundXML/Three"/>-->
<xsl:copy-of select="$inboundXML/Three"/>

package com.aviva.nu.cv.misctests;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.Element;
import org.dom4j.Attribute;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.*;

import java.io.*;
import java.util.Vector;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;

import org.apache.xpath.*;
//import org.w3c.dom.Document;
//import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.InputSource;

public class XSLTParamTest {

   private final static String DEBUG_OFF = "Off";
   private final static String DEBUG_ON = "On";
   String stringEncoding = "ascii";
   String debug = DEBUG_OFF;

    * Method main.
    * @param args
   public static void main(String[] args) {

File sourceXMLFile = null;
File paramSourceXMLFile = null;
String xslFilename = "E:\\Data\\WBIMB\\Projects\\Java\\Misc\\com\\aviva\\nu\\cv\\misctests\\experiment4.xslt";

try {
sourceXMLFile = new File("E:\\Data\\WBIMB\\Projects\\Java\\Misc\\com\\aviva\\nu\\cv\\misctests\\OddsAndEvens_NoNS.xml");

paramSourceXMLFile = new File("E:\\Data\\WBIMB\\Projects\\Java\\Misc\\com\\aviva\\nu\\cv\\misctests\\OddsAndEvens_NoNS.xml");

SAXReader reader = new SAXReader();

Document paramDoc = reader.read(paramSourceXMLFile);

               // now for the transform
	Transformer transformer = null;
	TransformerFactory tFactory = TransformerFactory.newInstance();

	transformer = tFactory.newTransformer(new StreamSource(xslFilename));

// set the param
//transformer.setParameter("inboundXML", "42");
transformer.setParameter("inboundXML", paramDoc.selectSingleNode("/Numbers/Odds"));

       	// do the transform
	StreamSource sourceDoc = new StreamSource(sourceXMLFile);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	transformer.transform(sourceDoc,new StreamResult(baos));

       	// show output
	byte[] data = baos.toByteArray();
	String outputString = null;
	try {
	            outputString = new String(data, "UTF-8");
	} catch (Exception e) {

       } catch (Exception ef) {

Current Thread