3

I am using client-side xslt to transform xml files into xhtml. There have been some hurdles but I have managed to get passed all of them except this.

The problem is that when I have a simple xml file like this

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

and transform it to xhtml with a simple xsl like this

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns="http://www.w3.org/1999/xhtml">

  <xsl:output method="xml"
  version="1.0"
  encoding="ISO-8859-1"
  indent="yes"
  omit-xml-declaration="no"
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>a title</title>
        <script type="text/javascript">
          alert(document);
          alert(document.anchors);
    </script>
      </head>
      <body>
        <xsl:value-of select="." /> world
      </body>
    </html>
  </xsl:template>    
</xsl:stylesheet>

the first alert will pop-up as "[object XMLDocument]" with firefox instead of "[object]" like it does for IE and safari. From what I gather this means that firefox does no produce a javascript html document (or html dom, not sure what the wording is). The second alert in firefox will be "undefined" but in IE and safari it is "[object].

So in firefox there is no document.forms or document.anchors etc. I know some javascript will still work, like document.getElementById, but I am afraid that more advanced stuff like ajax will not work propery if document.forms and the like do not exist.

Is there a work-around for this? On my current project I am rewriting a bunch of pages to use xslt. There is a lot of javascipt already writen and changing it all to use the limited firefox javascript is not really an option if it is even possible.

Thank you very much for any help.

  • oh ... please don't... how can you check that the HTML generated is valid? why doing it on client side? (besides performance...) – elcuco Jun 26 '09 at 22:22
  • "On my current project I am rewriting a bunch of pages to use xslt." Why? – John Kugelman Jun 26 '09 at 22:29
  • I am doing the transformations client side for performance. The html and the transformations aren't terribly complicated so it should all validate. For older browsers without support for xslt and for google bots I will do the transformation server side. –  Jun 26 '09 at 22:33
  • John, the pages are being rewriten to use xslt because the current version is writen using a strange php template system that we have outgrown. Xslt is the perfect solution for what we are trying to accomplish. Trying to do it all client side is the only real hang up. –  Jun 26 '09 at 22:36

2 Answers2

4

1) Fixing your problem

Solving your issue is as simple as changing value of the @method attribute from "xml" to "html" on xsl:output element.

2) Explaining the difference

HTML DOM extends core XML DOM interfaces. So, for example, the collection "forms" is not present in the XMLDocument, but is in HTMLDocument

Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56
  • Thanks. This problem has wasted too much time and your hint was finally solving lots of issues here. – soletan Mar 21 '12 at 16:50
0

The reason I used xml was I wanted to use xhtml for the output. Since I am doing the transformations on the client side I am limited to xslt 1.0 and xhtml is not an option. I had seen on several sites that the way to output xhtml was to select xml and use the omit-xml-declaration. I guess this is what was causing firefox to create the xml DOM.

Following Sergey's advice I changed my output method around and everything seems to work. This is what it looks like now

  <xsl:output method="html"
  encoding="ISO-8859-1"
  indent="yes"
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

I checked out the doctype in IE. It still says it is xhtml even though the method is html. I don't know why so many sites suggest the xml output method hack...

Thanks for explaining the difference with the xml and html DOMs. Out of curiosity, is there any way to manually create a html dom from the xml dom?

  • I could not bump up Sergey's post or comment on it since i don't have enough rep. I had to create an answer to continue the discussion. –  Jun 29 '09 at 14:25