jQuery in Action, 2nd edition*
The moose likes XML and Related Technologies and the fly likes Parameter passing in xslt transformation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parameter passing in xslt transformation" Watch "Parameter passing in xslt transformation" New topic
Author

Parameter passing in xslt transformation

Nirjhar Ghosh
Greenhorn

Joined: Oct 31, 2007
Posts: 2

I have a requirement that I need to pass a string that will be converted to a nested element parameter. But when I tried to pass the same it gives error as "Cannot convert a string to a nodelist".

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>
<xsl:param name="employees"/>
<xsl:param name="ISBN"/>
<xsl:template match="/">
<xsl:for-each select="$employees">
<xsl:sort select="$ISBN"/>
<xsl:value-of select="$ISBN" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="sort_CFJ.xsl"?>
<employees>
<employee hireDate="04/23/1999">
<title ISBN="15" author="David Rogers">
An Introduction to NURBS: With Historical Perspective</title>
<last>Hill</last>
<first>Phil</first>
<salary>100000</salary>
</employee>
<employee hireDate="09/01/1998">
<title ISBN="17" author="Gerald Farin">
NURBS: From Projective Geometry to Practical Use</title>
<last>Herbert</last>
<first>Johnny</first>
<salary>95000</salary>
</employee>
</employees>

From Main Program :

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer(new StreamSource(xslID));
transformer.setParameter("employees", "employees/employee");
transformer.setParameter("ISBN", "title/@ISBN");
transformer.transform(new StreamSource(new File(sourceID)),new StreamResult(System.out));

Please let us know how can I pass parameter in the situation. I tried with "#employees/employee" still it didnt work.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

Well, first of all you are passing a string as the parameter (like the error message says). But it looks like you're trying to pass an XPath expression, and XSLT can't interpret a string as an XPath expression. It has to be given the XPath expression at compile time. So you'll have to try some other strategy, what you are trying to do won't work.
Anish Kuti
Greenhorn

Joined: May 12, 2008
Posts: 29
for this kind of requirement you need to use xalan.jar
Nirjhar Ghosh
Greenhorn

Joined: Oct 31, 2007
Posts: 2
I want to use XSLT JAXP only not Xalan.
I tried to provide the NodeList in setParameter argument however this didnt solve the problem instead at runtime I am
getting the error message "Cannot convert a String to a Nodelist".
Tried with
1. setParameter(<name>,<String value of XPath>)
2. setParameter(<name>,<NodeList>)
3. setParameter(<name>, <Element>)
but none of them worked.
Can anyone please let me know exact code scriptlet.
Thanks
Anish Kuti
Greenhorn

Joined: May 12, 2008
Posts: 29
I have tried the same you have done.. But no success. Please share if you got any solution
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18716
    
    8

Nirjhar Ghosh wrote: Can anyone please let me know exact code scriptlet.


There is no code to do that. You've already done the experiments and proved that to yourself anyway. And besides, as I already said, there is no point in passing a node set. What you want to do can't be done in XSLT.

You don't believe me? Try the XSLT FAQ, the "Things XSLT can't do" section: http://www.dpawson.co.uk/xsl/sect2/nono.html

It's number 2 on the list.

(And no, using xalan.jar won't help. The standard Java XSLT transformer is already Xalan anyway, but choosing a different implementation wouldn't help anyway because XSLT can't do what you want. No implementation of XSLT can do what you want.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameter passing in xslt transformation