Apply XSLT to an XML file
Asked Answered
B

8

18

I realise that I will probably regret asking about this for the rest of my life, but... Is there some way of applying XSLT to an XML file without the XML file having an explicit reference to the XSLT file?

Personally, I thought the whole point of XSLT is that you can apply several different transformations to the same raw XML file to produce several different results from it. But that doesn't really work if the transformation has to be specified in the source XML file. It seems that to change the transformation, you have to change the underlying raw data file, which just seems wrong...

So is there some way to create some sort of file that says "take this XML and this XSLT and render the result in a browser window"?

Edit:

Perhaps my question was unclear.

If I open Notepad, write an XML file, and mention the name of an XSLT file within it, then when I double-click the XML file, the web browser applies the specified XSLT. Is there some way I can persuade the browser to do this without altering the original XML file? Or am I going to be forced to search for a command-line XSLT processor?

Batchelor answered 15/2, 2012 at 16:33 Comment(3)
Unfortunately, no one quite understood OP's question. I do, and I have the same one. The answers assume access to a server-side XSLT processor, but the OP scenario is 100% client-side XSLT processing. When a web browser opens an XML file, it looks in that file for a ?xml-stylesheet processing instruction that tells it what XSLT stylesheet should be used. But how can the browser be told what stylesheet to use without specifying it in the XML? A JS wrapper? an XML wrapper? a querystring parameter? a default XSLT in the web directory?Weakling
You can always invoke an XSLT transformation from an inline javascript (<script>) and there specify the URI for the primary XSLT stylesheet module. If you want just to double-click on the XML file, there is no-way for the browser to know even that you want the XML document to be transformed using XSLT. Finally, if the XML file actually contains an XSLT stylesheet module, and it starts with an xml-stylesheet PI pointing to itself, then the browser will apply the transformation on itself -- the transformation may know where to find the XML document and it can even be imbedded in it.Sweatbox
Were you looking for something like what Ido Weinstein (the bottom response) provided in How to link up XML file with XSLT file??Fronton
S
19

Is there some way of applying XSLT to an XML file without the XML file having an explicit reference to the XSLT file?

Of course. In fact the XSLT specification doesn't rely (mention) at all on the XML file having a reference to the XSLT stylesheet to process it.

Thus it is possible for the same XML file to be processed by many, different XSLT transformations.

In XSLT 2.0 and up it isn't even required for an XSLT transformation to have a corresponding XML document to be applied upon.

How this can be done?

The short answer: This is implementation dependent -- read the corresponding XSLT processor documentation (e.g. XslCompiledTransform for .NET, Saxonica for Saxon, ..., etc).

Also, almost every XSLT processor has a command-line utility for invoking the transformation from the console window -- again check the respective documentation (msxsl.exe for MSXML, nxslt.exe for XslCompiledTransform, ..., etc.)

Here are some comannd-lines for XSLT processors I am using:

This invokes the MSXML 3 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '3.0' -t %param[ name="value"]%

This invokes the MSXML 4 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '4.0' -t %param[ name="value"]%

This invokes the MSXML 6 processor:

msxsl.exe %xml% %xsl%  -o %out% -u '6.0' -t %param[ name="value"]%

This invokes .NET XslCompiledTransform:

nxslt2.exe %xml% %xsl% -t  -o %out% %param[ name="value"]%

This invokes AltovaXML (XML-SPY) for XSLT 10:

 AltovaXML.exe -xslt1 %xsl% -in %xml% -out %out%%param[ name="value"]%

This invokes AltovaXML (XML-SPY) for XSLT 2.0:

 AltovaXML.exe -xslt2 %xsl% -in %xml% -out %out%%param[ name="value"]%

This invokes Saxon 9.x (for XSLT 2.0):

java.exe -Xms512M -Xmx512M  -jar C:\xml\Parsers\Saxon\Ver.9.1.0.5\J\saxon9.jar   -t  -repeat:1 -o %out%  %xml%  %xsl%  %param[ name=\"value\"]%

This invokes XQSharp (XSLT 2.0):

XSLT.exe -s %xml% -o %out% -r 1 -t   %xsl% %param[ name="value"]%

In all of the above, %xml% is the path to the XML file, %xsl% is the path to the primary XSLT file, %out% is the path to the file that will contain the output from the transformation.

%param[ name="value"]% is a list of name = value parameter specifications and this isn't mandatory to use.

Sweatbox answered 15/2, 2012 at 17:12 Comment(2)
Dear Dimitre Thank you for your explanations, I am having the same problem. I perfectly understood your explanations, yet I'm wondering whether there is an even simpler way to "convert" an xml to html and display it in a browser without altering the xml source file. It seems to me, that the browser really has the facilities to process an xml and xls to produce html output, I just don't know, how to get the browser to choose a particular xsl without altering the xml code :) I hope you understand what I mean ... Thank you !Consueloconsuetude
@MischaObrecht, Yes, the browser-supported XSLT processor can be invoked inside a page by using Javascript -- but in this case you'll have to alter the Javascript of the page. So, I don't think that I gave you a bad advice.Sweatbox
H
1

Why of course! :)

You simply need to invoke your desired XSLT processor supplying (at a minimum) the XSLT and the XML file to use. In fact internally this is what applications like Internet Explorer do explicitly when they detect that an XML document has referenced an XSLT file.

How you do this will depend on your environment, for example there are command line XSLT processors and you can also apply an XSLT in most programming languages, e.g. Applying an XSLT using C#.

Halley answered 15/2, 2012 at 17:27 Comment(0)
S
1

Simple method here

http://www.devguru.com/content/technologies/xml_dom/16058.html

This method supports both standalone and embedded style sheets, and additionally provides the ability to run a localized style sheet fragment against a particular source node.

This method processes this node and its descendants using the specified XSL stylesheet, and returns the resulting transformation.

<html>
<head>
<script> 
    xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xmldoc.async = false; 
    xmldoc.load("xmldoc.xml"); 
    xsldoc = new ActiveXObject("Microsoft.XMLDOM"); 
    xsldoc.async = false; 
    xsldoc.load("xsldoc.XSL"); 
 </script> 

 <script> 
    document.write(xmldoc.transformNode(xsldoc)); 
 </script>
</head>
<body> 
Santana answered 29/4, 2017 at 15:42 Comment(0)
C
1

For those who - like me - were looking for an answer applicable to MacOS:

xsltproc transform.xsl data.xml > output.txt

MacOS comes with xsltproc. You can run "which xsltproc" and it will show you where it is installed.

Cattycornered answered 10/12, 2021 at 4:16 Comment(0)
L
0

Yes, it is possible. All XSL-T implementations have API to execute XSL-T transformation over an XML file.

Lyonnais answered 15/2, 2012 at 16:42 Comment(0)
G
0

Well, it seems that you're looking for some sort of ready solution. Instead of writing Java or .NET code to run XSLT on XML file. In Java world there is http://en.wikipedia.org/wiki/Apache_Cocoon library that allows that.

Gingras answered 15/2, 2012 at 16:48 Comment(0)
D
0

Responding to the OP's edit requirement to pick different XSLTs from the browser.

I didn't do any investigating before answering, but way back when IE was the only practical game in town, I remember there was a default CSS that got applied to XML files in the browser. Since CSS and XSLT files are specified in documents with a processing instruction, might there be some way through plugins/extensions to arbitrarily override the browser's default stylesheet - assuming other browsers handle this in a similar way?

Or might there be a way to dynamically add the processing instruction to the document and refresh?

Disappoint answered 3/3, 2012 at 16:34 Comment(1)
IE's stylesheet can be found with the URL res://msxml.dll/DEFAULTSS.xsl in IE.Winifred
B
0

All of those responding to this question (including me) seem to be unsure of what exactly you are asking. As you haven't marked any of the answers as being correct yet, I will go out on a limb and suggest that what you might be looking for is a HTML/AJAX(Asynchronous JavaScript and XML) solution. This way you can tell the browser to use a different XSLT style-sheet depending on certain conditions (eg. the browser is IE, or the date is New Years day, etc.).

Instead of pasting the code here, I will point you to a blog post which I believe covers exactly what you are looking for (you will need to add the conditions yourself): http://johanlouwers.blogspot.co.uk/2011/05/render-xml-into-div-using-ajax-and-xsl.html

Note: The example above uses two XML Docs & one XSLT Stylesheet, but can be easily adapted to use two Stylesheets & one XML Doc.

Note: If you run the example do it using your browser directly, as I have found launching the test.html file from an IDE like WebStrom results in the noting happening when the links are clicked.

If this answers your question (or is the closest to answering it), please mark it as the chosen answer so this post can be listed as solved.

Batista answered 3/4, 2015 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.