views:

1018

answers:

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"&gt;

  <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"/&gt;

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
      <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.

A: 

Try

document.anchors = document.getElementsByTagName('a')
alert(document.anchors)

document.anchors is old but you might be able to work around the problem by assigning it as a property.

steamer25
what do you think of this: if (!document.anchors) document.anchors = document.getElementsByTagName('a')
elcuco
Yeah after re-reading and seeing that Rico has a bunch of existing JS, I agree. I'm not sure if you need to check first--might work fine in IE too if you just assign it ;) . If it does bust IE, your solution looks good.
steamer25
I tried something just like this but I don't think it works. I replaced the script in the xsl with this<script type="text/javascript"> alert('document = ' + document); if (!document.anchors) document.anchors = document.getElementsByTagName('a'); alert('document.anchors =' + document.anchors);</script>In firefox i get 'document.anchors =[object HTMLCollection]'In IE i get 'document.anchors =[object]'If any of my existing js tries to access document.anchors i would imagine this would cause errors.This might work if i could somehow redeclare the entire document object.
+3  A: 

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
A: 

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"/&gt;

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.