File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes How to set XSL value from User Input in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to set XSL value from User Input in Java" Watch "How to set XSL value from User Input in Java" New topic
Author

How to set XSL value from User Input in Java

Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
I have an test.xsl XML schema which is generated from another xml file called ref.xml
ref.xml


test.xsl


the output of this xml schema is:



I used to transform xml schema to xml file using java servlets

in my xml schema I select two elements like first name and salary, instead suppose I have 50 elements in my original xml document but I want only 25 elements only which it is select by user by using user interface like checkbox in servlet
in xml schema
<xsl:value-of select="firstname" /> -> I want to select elements from checkbox in jsp page it will affect in this xml schema
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The usual way to pass information to an XSL transformation (or stylesheet) is via a top-level xsl:param element. In other words, as a parameter. In your Java code you would pass the value of such a parameter to your Transformer via its setParameter method.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
Paul Clapham wrote:The usual way to pass information to an XSL transformation (or stylesheet) is via a top-level xsl:param element. In other words, as a parameter. In your Java code you would pass the value of such a parameter to your Transformer via its setParameter method.



ya i got the output thanks but another problem is i want to set <xsl:element> also dynamically from java
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I guess that means you want to know how to use the value of that XSL parameter inside the XSL?

If your parameter was declared like this: <xsl:param name="e1"/> then the value of the parameter is $e1. For real programming you should use meaningful variable names, though.

And if you had a different question then please ask it.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
In my jsp page i apply checkbox like for 4 fields
firstname,lastname,nickname,salary
so in my servlet i get these value like String checkname[]=request.getParamaterValues("check"); how can i send selected checkbox values to xslt ?
before i did for sample i get a value from textbox and pass to xslt by tranformer.setparameter("e1",tbxvalue) but now i send multiple values means what i want to do in my java program as well as in xsl sheet please give me the solution
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Guessing again... if you need to use four parameters then go ahead and do so. Remember to give them different names.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
am just mention eg here in jsp page i make display 50 tag names with check box , which it is get from xml document, i will to check 10 elements which these values send to xsl sheet,now my question is how to send multiple checked values to the xslt sheet i use transformer.setparameter also as last post i mention and also in xslt sheet i want to make display 10 tag names with attribute values of first xml document
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Durga Roobini wrote:my question is how to send multiple checked values to the xslt sheet i use transformer.setparameter also as last post i mention


Right. You use one parameter for each of these "checked values".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Oh, now I think I understand what you are trying to say. You have a list of choices which are check boxes. The user might check some of them. You want your transformation to know which ones were checked. Right?

So I suggest your transformation could have one parameter for each of the choices. For the ones which are checked, pass a non-blank value into the transformation. For the ones which aren't checked, don't.
Durga Roobini
Ranch Hand

Joined: Dec 19, 2013
Posts: 105
how to send multiple parameters to xslt,for eg String checkname=request.getParamaterValues("checkname");-->it get from jsp page and in transformation part shall i use
for(String s:checkname){
transformer.setParameter("c1",s);
}
like this?
1.how to send 10 checked values to xslt ?
2.in xslt receive 10 external parameters from servlet and fetch the value of original xml document?

please i explain at my level of best give me the solution
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 511
    
    3
The indeterminate number of parameters passed into the xslt poses somehow problem. By the signature of the setParameter method, the transformer should take in object in general sense. But the implementations vary. Xalan would have major difficulty in taking array passed in, whereas Saxon's transformer engine would do a better job.

If you set the saxon as your transformer engine, you can do much simpler like this.
[1] First retrieve the reqest parameter values:

[2] Then you pass it to the parameter, say, c.

[3] In the template xslt, you retrieve the individual entry simply like this.


If you use xalan-j engine, you can do as well only this time it is super laborious.
[1'] You concatinate each entry in the String[] checkname with some clearly defined separator (avoiding possible character in the entry, say, ~).
[2'] You pass the serialized String[] with separator (say, ~) to the xslt.
[3'] In the xslt document, its design is under your complete control, you use algorithm with named template to individually re-discover each entry and do the work with it. (This is xslt 1.0 though. Again in xslt 2.0, you have tokenize() at your disposal and facilitates the work enormously. But xalan-j does not support xslt 2.0 for the moment, so that is unfortunately out of question.)

The above is what I would suggest you do or ask somebody to help you set it up in view of your way of asking question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set XSL value from User Input in Java