aspose file tools*
The moose likes XML and Related Technologies and the fly likes Using XSL to filter XML & Multiple XSLs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Using XSL to filter XML & Multiple XSLs" Watch "Using XSL to filter XML & Multiple XSLs" New topic
Author

Using XSL to filter XML & Multiple XSLs

Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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 ]

Mani
Quaerendo Invenietis
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Tony Yan
Ranch Hand

Joined: Apr 10, 2002
Posts: 170
(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


Tony Yan<br /> <br />IBM Certified Developer XML and Related Technology<br />Sun Certified Web Component Developer For J2EE Platform<br />Sun Certified Programmer For Java 2 Platform
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
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

Joined: Apr 10, 2002
Posts: 170
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

Joined: Nov 14, 2000
Posts: 1953
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using XSL to filter XML & Multiple XSLs