[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"?>
<rootContainer>3</rootContainer>

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 ??

Thanks

Fraser.

Source XML Instance for the param
======================

<?xml version="1.0" encoding="UTF-8"?>
<Numbers>
	<Odds>
		<One>1</One>
		<Three>3</Three>
		<Five>5</Five>
	</Odds>
	<Evens>
		<Two>2</Two>
		<Four>4</Four>
		<Six>6</Six>
	</Evens>
</Numbers>

Stylesheet
=======

<?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="/">
<rootContainer>
<!--<xsl:value-of select="$inboundXML/Three"/>-->
<xsl:copy-of select="$inboundXML/Three"/>
</rootContainer>
</xsl:template>
</xsl:stylesheet>


Java
===
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));
	transformer.clearParameters();

// 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
	baos.close();
	byte[] data = baos.toByteArray();
	String outputString = null;
	try {
	            outputString = new String(data, "UTF-8");
	            System.out.println(outputString);
	} catch (Exception e) {
	            System.out.println(e.getMessage());
	}

       } catch (Exception ef) {
           ef.printStackTrace();
       }
   }
}

Current Thread