capitano666
capitano666

Reputation: 656

XML and XSL transformation

On my tomcat I have a servlet at this address: http://192.168.1.2/FirstApp/TestXslServlet which generates the following XML:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="./TestBean.xsl"?>

<TheRoot>
    <hw>Hello World!</hw>
</TheRoot>

I have the xsl here: http://192.168.1.2/FirstApp/TestBean.xsl Which contains the following text:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/TheRoot">
  <html>
  <body>
  <h2>XSL loaded!</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th><xsl:value-of select="hw"/></th>
    </tr>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

When I load the servlet all I see is "Hello World!", which is the same I see if I completely remove the xsl or if I put a dummy xsl name, so it appears that there is some problem with loading it. Why?

Both the XML and the XSL should be correct as I tested them on this website: http://markbucayan.appspot.com/xslt/index.html and the transformation went as expected.

EDIT: It appears to be working on explorer, but not on firefox, so the issue is browser-related.

EDIT2: I tried to download both files from firefox to test them locally, the servled downloaded as TestXslServlet.xml and the xsl downloaded as TestBean.xsl.xml. I renamed the xsl removing the ".xml" extension and now from the local disk it's working fine. I tried to see if the firefox developer console could shed any light on what it's happening, if I reload the page on the server I get:

[11:01:39.325] GET http://192.168.1.2/FirstApp/TestXslServlet [HTTP/1.1 200 OK 32ms]

If I reload the page from the local disk I get:

[11:04:00.507] file:///C:/[...]/xsltest/TestBean.xsl

The MIME Type for TestBean.xsl is "application/xml" when loaded from the server and "text/xml" when loaded from the disk. I tried to upload the exact same files that works on local to the server and they do not work (the difference is that I'm now calling a static TestXslServlet.xml instead of the servlet to generate the xml, the MIME type is "application/xml", as for the servlet).

EDIT3: In order to rule out any mistake on my part I tried to use the example files provided here: https://developer.mozilla.org/en-US/docs/XSLT_in_Gecko/Basic_Example the page loads fine when loaded from the local disk but the transformation isn't applied when loaded from the server. It appears the be an issue with firefox(v.25) and tomcat(v.7) however the MIME type seems correct to me, from firefox's "page info" it shows "application/xml" for both (when loaded from the server, there is no MIME type information when loaded from local disk).

Upvotes: 3

Views: 959

Answers (2)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25044

Your XML is indeed well-formed, and your XSLT does run, when properly invoked.

I'd guess that your Tomcat server is serving FirstApp/TestXslServlet with a non-XML MIME type. If you want a browser to run the associated XSLT stylesheet on a Web resource, then you want it to be served as text/xml or application/xml (many authorities prefer the latter, which suggests they think XML is not human-readable text; I prefer the former), or some other MIME type which browsers recognize as XML and for which they will invoke a browser an XML parser and XSLT processor [Thanks to LarsH for spotting the typo]. At a guess, your configuration defaults to serving your servlet output as text/html.

Upvotes: 2

capitano666
capitano666

Reputation: 656

I've found the answer here: Firefox 3 doesn't apply my xslt stylesheet, but other browsers do

The "NoScript" plugin for firefox prevents XSL Trasformation...

Upvotes: 2

Related Questions