开发者

How do I alter the XML DOM with javascript, when using XML+XSLT client side

For example:

XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="example.xsl"?>
<document>开发者_StackOverflow社区
  <foo>bar</foo>
</document>

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <title>example</title>
        <script type="text/javascript">
          /* magic javascript that alters the foo-node to contain something else */
        </script>
      </head>
      <body>
        <p>Foo: <xsl:value-of select="foo" /></p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I want that javascript to modify the XML DOM, NOT the HTML DOM!


It's impossible, and even if it were possible, I have no idea what you'd expect it to do.

The XML has already been transformed for display, the XSLT doesn't keep updating the view in real time.

View Source in a browser will only show the source as-sent, rather than a serialisation of the DOM as-modified.

Debuggers such as Firebug will show a serialisation of the DOM as displayed, in the browser (i.e. the transformed DOM, plus any scripted modifications).


I think that's not going to be possible. By the time the Javascript runs, the XSLT transformations have already been applied, and for all intents and purposes the XML is simply gone.

Think about it: the only way your Javascript will even run is when the HTML parser sees your <script> tag and understands its semantics. That won't happen until after the transformation.

I haven't done that much work with XSLT via browsers before, so you should keep the flame of hope alive that some 100K user will drop in and show me up.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜