Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on setPrarameter() fror transformer..

 
Raj Sethi
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..
 
Alexander Sack
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic