• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using XSL to filter XML & Multiple XSLs

 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two questions here
Q1: Is it possible to get a subset of data from a XML file using XSL alone?
Example: If I have a XML template like the one given below

Say when I open the XML file in a browser, that XSL should make sure that only a subset of data (say where SEX=M or where AGE > 20) will be displayed. Possible?
Q2: I have 1 XML file and 2 XSL file. I want the XML file to use the first XSL at times and the second XSL file at other times. It should happen dynamically. How to do it?
Restrictions
-I can't use Server side processing (Servlets / JSP / ASP...)
-I can't modify the XML file (or atleast, I dont want to modify it)
[ October 23, 2003: Message edited by: Mani Ram ]
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1: Is it possible to get a subset of data from a XML file using XSL alone?
Yes, that's entirely possible. The output of an XSL transformation intended for visualizing the data rarely preserves all information.
Q2: I have 1 XML file and 2 XSL file. I want the XML file to use the first XSL at times and the second XSL file at other times. It should happen dynamically. How to do it?
Restrictions
-I can't use Server side processing (Servlets / JSP / ASP...)
-I can't modify the XML file (or atleast, I dont want to modify it)
The only way I can think of is to make the first XSL (referred to by the original XML file) generate another XML document, which in turn refers to the second XSL stylesheet. However, I really don't know whether the browsers support this kind of double-transformation.
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(q1) There are many ways, (a) xsl:key (b) xml query (c) brutal force, for-each looping and checking (d) ...?
(q2) When you say at times and other times, what do you mean? where is this information/criteria for determing which xsl to use? If it exists in the XML as a value, then you can use one XSL and then choose the style depending on that value inside that XSL. If it is outside of the XML, then your app should know which xsl to use. If you need random selection, your app should decide it, not XML or XSL.
my 2 cents
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions.
Here is my actual problem. There is a XML file like the one given below (currently it doesn;t have any XSL associted).

Now, I have to build a HTML page with an option for the user to select the country he wants to view. When he submits the form, I have to display this XML, but only containing the data which matches the country entered by the user. (If he selects India as the country, only the first and the fifth set of the above data should be displayed).
How to do this (with out using any server side tasks)?
 
Tony Yan
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like:
<xsl:copy-of select="//CITY[COUNTRY='India']"/>
Where //CITY picks up all the CITY nodes and content in [] leaves only COUNTRY equals to 'India' nodes. Copy-of should dump everything from CITY Downward.
Regards,
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need two or more xslt files. You can use parameter to pass country user selected to the xslt to solve your problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic