Two Laptop Bag*
The moose likes XML and Related Technologies and the fly likes splitting xsl file :creating dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "splitting xsl file :creating dynamically" Watch "splitting xsl file :creating dynamically" New topic
Author

splitting xsl file :creating dynamically

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I have a sample xsl file like below. I want to do 2 things,

First i want to split the below xsl file into 4 xsl files.One for State Info ,one for Location info and another for Country Info.This way i will be able to maintain code easily. Is there something like i can do a import
Second, imagine that i come up with smaller 4 xsl files like a)SchoolStateInfo.xsl b)SchoolLocation.xsl c)SchoolCountryInfo.xsl d)SchoolInfo.xsl. Now i want to create an xsl dynamically. I mean if the User selects,say
"school state" and "school country", then then i shold merge "schoolstate.xsl" and "schoolcountry.xsl".

hope iam clear.(basically i want to create xsl dynamically)






Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, there's an <xsl:import> element. It isn't hard to find that out by some basic investigation.

As for creating new XSL files, sure, why not? They are just text, or XML, so you can work with them as text or as XML. Whether it's useful to do that is another question, but you said nothing about why you would want to do it so there isn't any point in suggesting anything.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thank Paul ! But don't you think i would have done some basic investigation before typing in such long post ? I mean,no one will post in a forum without investigating,specially if the post is as long as written by me. Basically i didn't wanted to make my post very long,so i tried to keep it as short as possible.

Let me tell you what i tried

i created a separate xsl file called say "all_templates.xsl"




And then imported the above xsl file into my original xsl file like below .See line 06(import) and 103 (apply imports) below




but the problem is that above gives compilation issues. Is it possible for you to give me a small working sample?
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
(Now the second part of the question)Adding to the above lets say I split the above xsl into four smaller xsl files.For eg as mentioned earlier a)SchoolStateInfo.xsl b)SchoolLocation.xsl c)SchoolCountryInfo.xsl d)SchoolInfo.xsl.

Now say my pseudocode is something like below



Above is ok if the user selects only one option .What if the user clicks on 2 or more options. For eg if User selects both "SchoolState" and "SchoolLocation", then i want to dynamically generate a xsl combining "SchoolStateInfo.xsl" and "SchoolLocation.xsl". Is there a way i can do this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Maan Suraj wrote:but the problem is that above gives compilation issues. Is it possible for you to give me a small working sample?


Well, the XML example you posted wasn't well-formed. That's going to give you problems. Whether they would be the same as the problems you actually had -- who knows? You didn't say anything about them except that they were "compilation issues". That isn't very helpful. Could you have a look at our FAQ document TellTheDetails, which should help you ask answerable questions?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Maan Suraj wrote:Above is ok if the user selects only one option .What if the user clicks on 2 or more options. For eg if User selects both "SchoolState" and "SchoolLocation", then i want to dynamically generate a xsl combining "SchoolStateInfo.xsl" and "SchoolLocation.xsl". Is there a way i can do this?


Sure there is. At least I expect there probably is -- it isn't clear what's supposed to happen in that case, but somebody who had proper specs could probably figure it out. But I wouldn't do it that way. I can think of at least two solutions to that problem which don't involve generating XSL at the time of the request.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Let's forget the compilation part.


At least I expect there probably is


I am also quite sure there should be a way to do this...but i am not getting it. I am new to xslt. That's the whole reason i am asking in this forum.There should be a way to do this...agreed,but what is it . I am quite sure you or people in this forum might have come across this scenario. I thought of using something like <xsl:param> or something but not sure if there is a better way to do that. I want something like "base.xsl", and based on params i want to combine the xsls. For eg if the param contains SchoolState and School Location, then base.xsl should import contents of SchoolStateInfo.xsl and SchoolLocation.xsl .

Similarly another eg can be if the param contains SchoolCountry and SchoolInfo then base.xsl should import SchoolCountryInfo.xsl and SchoolInfo.xsl content. So i need to figure out how can i do that.I mean all these permutations.

it isn't clear what's supposed to happen in that case
Please tell me what is not clear to you?. Hope above helps.

I can think of at least two solutions to that problem which don't involve generating XSL at the time of the request.
What are those?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Maan Suraj wrote:I am also quite sure there should be a way to do this...but i am not getting it. I am new to xslt. That's the whole reason i am asking in this forum.There should be a way to do this...agreed,but what is it . I am quite sure you or people in this forum might have come across this scenario. I thought of using something like <xsl:param> or something but not sure if there is a better way to do that. I want something like "base.xsl", and based on params i want to combine the xsls. For eg if the param contains SchoolState and School Location, then base.xsl should import contents of SchoolStateInfo.xsl and SchoolLocation.xsl .

Similarly another eg can be if the param contains SchoolCountry and SchoolInfo then base.xsl should import SchoolCountryInfo.xsl and SchoolInfo.xsl content. So i need to figure out how can i do that.I mean all these permutations.

it isn't clear what's supposed to happen in that case
Please tell me what is not clear to you?. Hope above helps.


What isn't clear is what is actually supposed to happen. Are we supposed to see the output of SchoolCountryInfo.xsl followed by SchoolInfo.xsl? Or something else? It does make a difference, you know. The usual way to design systems is (1) Figure out what you want to do (2) Figure out how to do that. You have inverted this process (which is probably why you are getting nowhere). And by the way this has nothing to do with XSL, this is always how you should design systems.

I can think of at least two solutions to that problem which don't involve generating XSL at the time of the request.
What are those?


Of course I have no idea what you want to do (remember I'm talking about step (1) here). I strongly recommend you work on that first. However I would look at passing parameters into an XSL which contains all of the templates you might need to complete your requirements. I don't see any need for building that XSL at run-time.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Let me try to go back and explain the problem a little further. The initial requirement was that the content of the 4 XSL files which i am taking about, were in one xsl file.To make my explanation easier,lets just consider 2 things
1) School Location and 2)School Info.

The contents for above two were in one xsl file(base.xsl). Based on the initial requirement,the user didn't had a choice to select params,say if he clicks submit button from UI,then all information (like school locations,school info etc etc ) will be retrieved (as xml) and the big base.xsl was used to map it to html.

Now the requirement has changed,user now has option to select params. He has a choice from UI to select Location,SchoolInfo etc. So i thought it's better for me to split the big base.xsl file into smaller files. I am calling base.xsl file as big xsl file because actually in real time i have some 8-10 templates.Here for making my explanation easier I am taking only 2 templates (i.e location and schoolinfo).

This is how initial base.xsl file looked like. Please don't go for syntax error in below xsl files.The idea is to explain the reqt only.



From maintaining pt of view,i thought it's better to split the above file into two smaller files. So first smaller file is schoolinformation.xsl



And the second xsl file is "schoolLocationInfo.xsl" file


What is my problem.

The above xsl works fine in few scenarios.So basically now if the param contains "schoolinfo" ,then i use "schoolinfo.xsl" to convert xml into html and i f param contains "schoollocation" then use "schoollocationinfo.xsl".Till here it was fine.But the problem is that if my param contains both "schoolinfo" and schoollocationinfo" .I mean the user has an option from UI to multiselect things. This time i have to merge contents of both xsl files into one,something like i had in my original base.xsl file.Here since its a small eg, we can probably get away with few if conditions in our code and call particular xsl file (i.e either shoolinfo.xsl, or schoolLocation.xsl or base.xsl).

PS . In real time i have atleast 8-9 such templates,so just imagine , the User has choice to select one params or as many as 7/8 params. So i want to import things dynamically based on params.Hope this explanation helps.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Paul Clapham wrote:What isn't clear is what is actually supposed to happen. Are we supposed to see the output of SchoolCountryInfo.xsl followed by SchoolInfo.xsl? Or something else? It does make a difference, you know.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Now the requirement has changed,user now has option to select params. He has a choice from UI to select Location,SchoolInfo etc. So i thought it's better for me to split the big base.xsl file into smaller files.

The stated requirement, "User has option to select params" really has nothing to do with splitting XSL files and creating them dynamically at runtime. This is something that you created in your mind as a possible improvement. It doesn't seem so however.

Aside, if you wanted to create an XSL file at runtime you most likely would be better off doing the conditional logic in Java or Perl code. This scenario adds a bunch of additional items such as keeing track of which templates are in which files, file-names, etc. All have nothing to do with the requirement. I image that the better option is to maintain a single stylesheet.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks jimmy. Yes,sure let me think of that.But do you see the problem. Just imagine how many if conditions i need to have in my java code. (There will be many)

What isn't clear is what is actually supposed to happen. Are we supposed to see the output of SchoolCountryInfo.xsl followed by SchoolInfo.xsl? Or something else? It does make a difference, you know.


yes Paul,you are right
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let me make it clearer. Let's suppose you had to write some Java code, and you expected to have to do a lot of different things based on the user's input. What would you do? Would you decide that the right thing to do would be to take the methods which were to be used for what the user input at run time, and build a Java class just for that? Of course you wouldn't. You would write a Java class which could handle anything the user was going to answer, and just call the methods you needed to call at run time.

You should do the same thing for XSLT. The idea of building a transform at run time is just as absurd as the idea of building a Java class at run time. In case you still don't get it, I am suggesting you should write an XSLT document with templates for all of the options that the user might select at run time, and only call the templates which the user actually does select.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Your class perspective makes sense. But what i thought was perhaps,there could be a way to import dynamic things,just like the way we do dynamic imports in jsps and all.

I am suggesting you should write an XSLT document with templates for all of the options that the user might select at run time, and only call the templates which the user actually does select.


Let me try out your suggestion as well.

Thanks for you suggestion and patience Paul .

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Maan Suraj wrote:But what i thought was perhaps,there could be a way to import dynamic things,just like the way we do dynamic imports in jsps and all.


That depends on what "dynamic" means. Usually when it's used in a forum question it means "by some process which I don't understand which might as well be magic". You already know about the <xsl:import> element. That is done at run-time (as is everything else in XSLT for that matter). Does that make it "dynamic" or is there some other requirement for it to really be "dynamic"?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A static XML element in a static XML document may not be "dynamic." If the element triggers something to happen in an XSLT Engine at runtime, the nature of the static element does not change.

In order to create an XML document "dynamically" you would need to create a software application which will do this. The complex logic for this sort of application would require expert-level knowledge, skills and abilities.

Attempting to create "XSL" at runtime using "XSL" templates seems a bit bizarre. There really is nothing "dynamic" about JSP import elements. The import element is static and resides in a static JSP file. The fact that a JSP Engine complies this file and a Servlet Engine executes the result, does not make the JSP import element "dynamic."
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
well thanks everyone for the clarification guys
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: splitting xsl file :creating dynamically
 
Similar Threads
XSL: test for first and last occurence of template match
How to dynamically create or modify XSL
Using XSL to display XML
java.lang. IllegalState Exception: Root element not set
Keyword xsl:output may not be used here.