• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Help on setPrarameter() fror transformer..

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!