This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Combining existing data with xml in a transform Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Combining existing data with xml in a transform" Watch "Combining existing data with xml in a transform" New topic

Combining existing data with xml in a transform

Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
I have a requirement to do a bunch of enrollment forms in the context of jsp. The same questions appear on many forms, but sometimes with different labels, or different options. So the same question could be marked up with one xsl in form A but another xsl in form B. So I plan to implement each question as an xml file:




The xsl will turn the question into an input, or a checkbox or a textarea and so on.

My jsp will look something like this:

<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="x" uri="" %>

<title>XYZ enrollment</title>

<div id="part1" class="question">
<c:import url="question_001.xml" var="question_001"/>
<c:import url="question_001.xsl" var="question_001.xsl"/>
<x:transform xml="${question_001}" xslt="${question_001.xsl}"/>

<div id="part2" class="boilerplate">
<c:import url="http://localhost:8080/boilerplate_001.xml" var="boilerplate_001"/>
<c:import url="http://localhost:8080/boilerplate_001.xsl" var="boilerplate_001.xsl"/>
<x:transform xml="${boilerplate_001}" xslt="${boilerplate_001.xsl}"/>

<div id="part3" class="question">
<c:import url="question_002.xml" var="question_002"/>
<c:import url="question_002.xsl" var="question_002.xsl"/>
<x:transform xml="${question_002}" xslt="${question_002.xsl}"/>

<c:import url="question_003.xml" var="question_003"/>
<c:import url="question_003.xsl" var="question_003.xsl"/>
<x:transform xml="${question_003}" xslt="${question_003.xsl}"/>


Response data will be captured in xml columns, so I have the option of exposing existing data through the view as xml, or Strings or lists or whatever.

What I don't know how to do is combine existing response data into the markup in some cases. For input fields that's easy, I can have the transform spit out the markup necessary to retrieve the field value from the view. What I don't understand is how to populate things like checkboxes. For example how can my transform know whether to insert the checked="checked" attribute if, for a <question> whose options are RED BLUE GREEN and whose presentation should be checkbox format, the existing data says the user selected GREEN?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19857

Usually you do that by passing parameters to the transform. I'm not sure if you can pass parameters to the transform via JSTL -- after a quick look around the web it looks like you can't, but you should do a more thorough look than what I did.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19857

I suppose you could make it work by having a servlet which generated a suitable XSL transformation based on input parameters, instead of a fixed transformation like you have there, but if I found myself going down that road I would think hard about whether there was a different approach.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Looking over this again I think most questions could be handled as well with tag files. A given question mat have different text on different forms but I think that can be handled adequately with tags. A completely different skin on the same question is more of an edge case. The boilerplate almost certainly needs transforms, but does not need params.
I agree. Here's the link:
subject: Combining existing data with xml in a transform
It's not a secret anymore!