aspose file tools*
The moose likes XML and Related Technologies and the fly likes Help on setPrarameter() fror transformer.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Help on setPrarameter() fror transformer.." Watch "Help on setPrarameter() fror transformer.." New topic
Author

Help on setPrarameter() fror transformer..

Raj Sethi
Greenhorn

Joined: Mar 11, 2001
Posts: 9
Hi All
I have the following code
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
public class TestTransform
{

private static String RULES=
"<?xml version=\"1.0\"?>" +
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" +
" <xsl:template match=\"*\">" +
" <xsl:element name=\"{name()}\" >" +
" <xsl:call-template name=\"template-name\">" +
" </xsl:call-template>" +
" </xsl:element>" +
" </xsl:template> " +
"<xsl:template name =\"template-name\">" +
" <xsl aram name=\"first\">First </xsl aram>" +
" <xsl aram name=\"last\">Name </xsl aram>" +
" <xsl:value-of select=\"$first\"/>" +
" <xsl:value-of select=\"$last\"/>" +
"</xsl:template>" +
"</xsl:stylesheet>";
private static String SOURCE="<a>test</a>";
public static void main(String args[])
{
Source xmlsource = new StreamSource(new ByteArrayInputStream(SOURCE.getBytes()));
TransformerFactory factory = TransformerFactory.newInstance();
ByteArrayOutputStream result = new ByteArrayOutputStream();
Transformer transformer = null;
try
{
transformer = factory.newTransformer(new StreamSource(new ByteArrayInputStream(RULES.getBytes())));
transformer.setParameter("first","Raj");
System.out.println("get parameter first:" +transformer.getParameter("first"));
transformer.transform(xmlsource,new StreamResult(result));

System.out.println("output :"+result.toString());
}
catch (Exception e)
{
System.out.println("error "+e.getMessage());
}

}

}

The out put is FirstName instead of RajName //because I'm setting param first to Raj at //transformer.setParameter("first","Raj");
Where I am getting it wrong appreciate any help..
Alexander Sack
Greenhorn

Joined: Jun 29, 2001
Posts: 3

Hi,
from java it seems fine; but you have to define the parameter
in a global scope ( child of the xsl:stylesheet tag ) not
in local scope (child of xsl:template element)

Alexander Sack

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help on setPrarameter() fror transformer..