File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Use different XSL files on the same XML doc 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 "Use different XSL files on the same XML doc" Watch "Use different XSL files on the same XML doc" New topic

Use different XSL files on the same XML doc

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
How do I use different XSL files on one XML document?

I have one XML file which I want to display in different ways. Therefore I have made i.e. 3 XSL files.

But I have this problem that the only way I know to use the XSL file is to include it in the top of the XML file, like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="info.xsl"?>

But in this way I have to make one XML file for each XSL file.

Is it possible to include the XML file in the XSL file instead or what are the solution?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

The way you are doing it now, you are relying on the browser to do the transformation for you. If you don't have anything on your server side that can do Java processing, then I think your only option is to produce three almost-identical versions of the XML file.

However if you have something at the server side that can dynamically insert that processing instruction, or that can actually do the transformation, then do that. But I won't get into explaining how unless there is a possibility the information might be useful.
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
I don�t have the possibilty do it in java. I just hoped it could be done in pure XML and XSLT!
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Is the stylesheet "hard coded" in an XML file you can not change?

OK, so you can not do Java. Can you run Java programs?

If you could do this from a "command line", would that work? (See Xalan examples at

nuthan kumar
Ranch Hand

Joined: Feb 14, 2006
Posts: 47
I hope this will help you.. we can transform XML into HTML using xsl in java script. ( see sample code below). by using this you can display same XML with three different xsl files.

<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false

// Load the XSL file 1
var xsl1 = new ActiveXObject("Microsoft.XMLDOM")
xsl1.async = false

// Load the XSL file 2
var xsl2 = new ActiveXObject("Microsoft.XMLDOM")
xsl2.async = false

// Transform


Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
I am working on a solution which is made in pure HTML/XML/XSL. The solution with loading the XML file from Javascript is working fine. Thank you.

I am used to code in JAVA where there is no constraints at all. I can see I am facing some new challenges now:

On the website I am displaying the three latest news from a XML document. Every news-story has its own ID (I use the RSS format). In my XSL I display every news title as a link, that will point to a new site and show only that news-story ( ).

Is that possible to do in pure HTML/XML/XSL?

How Can I get the newsID parameter on another site and how can I send this parameter to my XSL file, so it can be used in a XPath expression?
[ July 11, 2006: Message edited by: Jeppe Fjord ]
I agree. Here's the link:
subject: Use different XSL files on the same XML doc
It's not a secret anymore!