Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Getting Netscape to use a linked XSL stylesheet

by AlistairFromScotland (Initiate)
on Sep 22, 2002 at 20:15 UTC ( #199947=perlquestion: print w/replies, xml ) Need Help??

AlistairFromScotland has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone help? I'm baffled!

I have a Perl CGI script that takes input from an HTML form and generates an on-the-fly XML page by extracting nodes from an XML file on the server - the choice of nodes varying, depending on what the user selects on the form. The Perl script prints the new XML page and sends it back to the browser with the following 1st two lines:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="http://localhost/my-stuff/stylesheet.xsl"?>

IE6 does what I want it to - which is: it notices the reference to the XSL stylesheet, so it uses that file to transform/format the XML into XHTML, resulting in a nice-looking web page.

However, Netscape 6 ignores the stylesheet and just displays the XML. But if I save that same XML as a file with a .xml extension it does the right thing and uses the XSL stylesheet.

My question is: How do I get Netscape to treat what I send it from the Perl script in the same way it would if it was reading the same thing from a save XML file? Netscape 6 obviously knows how to use XSL, but it just doesn't want to do it when it gets passed the XML from a Perl CGI script.

How do I get Netscape to act like IE? How do I get it to use the linked XSL file?

Thanks in advance

  • Comment on Getting Netscape to use a linked XSL stylesheet

Replies are listed 'Best First'.
Re: Getting Netscape to use a linked XSL stylesheet
by BUU (Prior) on Sep 22, 2002 at 20:29 UTC
    I would assume (aka blind guess) that you would need to send a "content-type: text/xml" or somesuch as your header.

    Btw, since we're more or less on the subject of xml/xlst, can you (or anyother passing monk) point me to a good beginners tutorial on transforming xml with xlst? Thanks.

      There's a good tutorial on

      "Why don't we ever challenge the spherical earth theory?"

Re: Getting Netscape to use a linked XSL stylesheet
by Flexx (Pilgrim) on Sep 22, 2002 at 22:28 UTC


    I think ++BUU is right. When you load the file via HTTP, Netscape has to believe of whatever content type the server claims the file is. And I'd bet that it gets text/plain as the content type (maybe that's actually what you send it through, or no content type, where it'll probably assume text/plain. And in a plain text file, it does not expect and thus not obey any processing instructions (like your stylesheet link)

    Now, if you save the file, and load it (as a file:// URI) in Netscape, it's on it own with determining the mime type. It sees the XML ending and assumes a text/xml content type. Being in XML context now, it sees, interprets and obeys the sylesheet PI.

    I wonder what the HTTP specs say about what to assume if no content type is given. If it says text/plain is to be assumed, then actually IE6 misbehaves. But maybe it allows (or even encourages) browsers to apply their own type recognitions, when the server doesn't tell. When I got time I'll eventually read into it, and update this node.

    Hope this lifts the fog,
    so long,

      I don't believe it's legal to have an HTTP response without a valid Content-Type. A text/plain content-type, though, is fairly common as a "fall-back" for most web servers.

      The problem here is that IE never trusts the web server. When it receives what it considers a "possibly too generic" content type, it will second-guess the web server and try to figure out the content type on its own, so if it sees a text/plain content-type, but something that looks XML or HTML-ish, it will interpret it as XML or HTML.

      Most people view this as really evil/stupid behavior, since it encourages misconfigured web servers. Neither Mozilla nor Netscape have adopted this practice, to my knowledge.

        Yep. I didn't mean it's okay to send no content type header. And I also think that IE is an instrument of evil, but I feared to be voted down for being too honest about my opinion ;)

        And I'm sure the followers of the Gates Monastery ;) will argue how smart a "feature" it is to realize an XML document even if the server wants it to be viewed as plain text (for whatever reason -- none of the browsers business...) They're like their browsers: They think they know everything better... ;)

        Sorry, couldn't resist.. ;)

        Cheers, Flexx

        BTW, I wrote this on IE 6... >;|

      Unfortunately, specifying the content type as text/xml does not solve the problem. Netscape still displays the XML as XML without applying the XSL (i.e. without transforming it into XHTML.


Re: Getting Netscape to use a linked XSL stylesheet
by joe++ (Friar) on Sep 23, 2002 at 15:48 UTC

    Apart from finding out whether the browser actually tries to load the stylesheet (server logs?) and to check the content type (mime type "text/xml"), there are two additional differences between MSIE and NS which could wreak havoc here:

    • Mozilla (Netscape's engine) does NOT parse generated html from a XSL tranformation, but rather takes the result tree from the transform literally and tries to render this. So you want to transform into valid xhtml
    • MSIE still tries to conform to an early, preliminary XSLT specification which predated the official W3C version. Mozilla doesn't and you shouldn't, because as of MSXML 3.0 (roughly MSIE 5.5, one of those service packs) Explorer is fully compliant with the correct XSLT spec.
    This - and more - is briefly documented at Mozilla's XSLT project page.

    Cheers, Joe

      Thanks, but I'm assuming that if Netscape 6.1 can handle the XML correctly (i.e. transform it to XHTML using the XSL file) when it loads it from a file, rather than being passed it from the CGI script, then the XML and the XHTML must be valid.

      It would be nice to be able to capture the XHTML produced by the XSL file and then run it on W3C's validator to check that it's 100% valid, but the XHTML gets generated invisibly inside the browser. Doing a "View Source" just shows the XML. However, I'm pretty sure it is valid XHTML.

      I've checked the Mozilla XSLT project page but it didn't help. It says that the stylesheet should be text/xml as well as the source, but if I use text/xml rather than text/xsl
      a) Netscape 6.1 won't transform it, even when I load the XML from a file
      b) IE won't transform it either.

      I've also checked that I'm using the correct namespace for xsl (as specified on the Mozilla XSLT project page).

      I've pretty much run out of ideas. Somebody else must have come up against this problem. There's got to be a solution out there.

      I could of course just do it the easy way and get the Perl script to write the HTML. But I want to do the transformation on the client side. And I keep hearing how Mozilla/Netscape is All-Good and IE is the Pure Evil, so how come it works flawlessly in IE whereas Netscape 6.1 just acts dumb and shows me the XML?


Re: Getting Netscape to use a linked XSL stylesheet
by Flexx (Pilgrim) on Sep 26, 2002 at 18:33 UTC

    Ok then... It's not the content type header. It's not validity. So we need to step back and refocus, I guess...

    Another idea: Don't use localhost in the href to the XSL. Use a relative link, or use your box's real hostname. Maybe your browser/server chokes on where http://localhost is, while it's fine with file://localhost.

    Hamlet: There's more between heaven and earth than man can apprehend, Horatio...
    Horatio: Like Quantum::Entanglement, huh?

    So long,

      Thanks, but this didn't work either. Mozilla still just displayed the XML.

      Maybe I should just admit defeat and say that this is a case of Internet Explorer perhaps not doing things properly, but at least doing them (unlike Netscape/Mozilla). In my opinion there's a lot to be said for getting things to work somehow rather than sticking to the rules and delivering something that just won't do what it's supposed to!

      Thanks again for everyone's suggestions. Please post if you can think of anything else I might try.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://199947]
Approved by Zaxo
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2022-11-28 01:39 GMT
Find Nodes?
    Voting Booth?