简体   繁体   中英

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>
  <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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM