Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameter passing in xslt transformation

 
Nirjhar Ghosh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for this kind of requirement you need to use xalan.jar
 
Nirjhar Ghosh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the same you have done.. But no success. Please share if you got any solution
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic