client side xslt with javascript in firefox

旧街凉风 提交于 2019-12-23 20:58:29

问题


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.


回答1:


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




回答2:


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?



来源:https://stackoverflow.com/questions/1051529/client-side-xslt-with-javascript-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!