views:

43

answers:

2

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

+1  A: 

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.

Pointy
+2  A: 

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).

Paul Butcher