Which browser can show XML data transformed by XSLT?
Asked Answered
B

6

26

say, if there is a file that's call data.xml, and a file that is format.xsl (or is it format.xml ?), which is to transform the XML data and format it as well using CSS, then which browser can display it? Which file should be opened? (the .xsl or the .xml?)

Actually I saw in another example that the XSLT file's first line is to href="format.xsl", so I thought the XSLT file is already the .xsl ? then how come it is linking to another .xsl file? How many files are there, 2 or 3?

Bar answered 12/8, 2010 at 10:53 Comment(0)
T
25

There are two files, one .xml containing data and one .xsl with the XSLT script for transformation. As of August, 2010, all leading browsers support client side XSLT transformation. Open the .xml file and the associated .xsl file will be used. Usually the script builds an html document which is displayed on the fly. The transformation can also be done by the server with PHP etc.
To associate a XSLT script, the .xml file needs a line like this:

<?xml-stylesheet type="text/xsl" href="script.xsl" ?>

Edit 18.04.2021: The question is 10 years old now and a lot has happened. It's possible now to have XSLT 3.0 running in the browser using Saxon-JS. There's a Hello World on Github.

Thermos answered 12/8, 2010 at 11:2 Comment(0)
H
11

Many browsers support the XML-stylesheet processing instruction. If it is included in an XML file and you open this XML with your browser, the browser will load the specified XSLT, run it with the XML file as input and display the XSLT's output instead of the original XML document. The spec can be found here: http://www.w3.org/TR/xml-stylesheet/

Wikipedia's XSLT entry has an example of how to use the processing instruction. Basically you just need to add this line at the top of your XML file (after the <?xml?> prolog), with 'example2.xsl' being a path to your XSLT file:

<?xml-stylesheet href="example2.xsl" type="text/xsl" ?>

Firefox and IE should both support this (and probably many other browsers, but I never tried - this feature is not used that often).

Haiti answered 12/8, 2010 at 11:2 Comment(4)
just tried the current Safari and Chrome and they both support it, although Chrome didn't open it if it is a local file.Bar
If the XSL is transforming the XML to XHTML, Chrome should open/transform a local file if the XHTML namespace is added to the XSL. I've had to do the same thing for FireFox in local XML files. See: #2982024Forsaken
This is because of security. It would be terrible if resources in internet can use files from your disk.Thickskinned
Firefox no longer opens the XSLT, but will display the content of the XML. Chrome, and consequently Edge, not only disallows the XSLT but will also not display the content of the XML. IE is now essentially dead.Delaryd
F
6

On mac, I tried opening the XML file with Chrome and Safari and neither displayed anything.

The solution that worked for me is to run xsltproc from command line to first get an HTML and then open the HTML with a browser. Here is the command:

xsltproc <XSL file> <XML file> > test.html

Now open test.html with browser.

Forrester answered 11/4, 2017 at 23:59 Comment(1)
xsltproc worked on macOS 10.14.5 nicely. I did have to edit the .xsl file to remove a block of javascript which xsltproc kept flagging with some kind of error that made no sense. Safari used to be able to open the XML when the XSLT was in the same directory without xsltproc 😖Jamilla
A
3

The best solution I have found is to use the VS Code "Live Server" extension. Just open the folder and you will get the desired output when you open the xml file linked to XSL file. How I got this Idea?

As Andrea said, don't forget to link the XSL file.

FYI I used Firefox to open LiveServer. I hope you can use any other browser.

Afterbody answered 15/11, 2022 at 7:16 Comment(0)
T
1

Firefox and Internet Explorer (definitely version 6 and up, likely earlier versions too) can parse XML/XSL well. You could actually write a client-side parser in Javascript, both those browsers work well with that.

You'll probably want to make sure that you don't use any vendor-specific pieces, tho, which can break compatibility (such as <msxsl:script>). The same might apply to other parsers (for example the node-set() function, which isn't the same between MSXML all parsers).

Within your XSL file, you may include another XSL file. Or more XSL files. That way, you can re-use templates from one XSL file in another.

Thee answered 12/8, 2010 at 12:44 Comment(0)
G
0

I believe support is partial in all browsers.

Here are some charts: http://greenbytes.de/tech/tc/xslt/

Gentille answered 26/1, 2016 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.