griefforyou [Original]
Dear Dr. GUI:
I have a question about XML. I publish an ASP page via XMLHTTP, send the data back as HTML, and do object.innerHTML on a div, table cell, or any element.
The problem I'm having is that once I create a form on an ASP server page and send the form back as HTML, everything works fine. But I also want client-side validation of the page, so I put a JScript page link into the body of the response HTML that I plan to send back. After doing this, the script will not run.
When I include the actual JScript src reference on the parent page, it works fine. Is this because the JScript code is not loaded into the runtime? Is it possible to respond to an XMLHTTP post without returning JScript code and have it work? Can JScript functions be generated on the fly for the pages I create?
Another way I haven't tried doing this is to post the XML data via XMLHTTP, then send it back as XML data, and then use an XSL stylesheet to transform it. I guess it's possible to define JScript functions that way. But this approach seems too complicated. Do you have any suggestions or answers?
Thanks for
the reply from Dr.
Peter Sung
GUI:Yes, Peter, you can return Microsoft JScript code in response to XMLHTTP post/get and have that code executed. Doing this is like getting a piece of the cake and eating it later. As you might guess, the best way to do this is to leverage an XSL stylesheet; this is easy once you get all the types back to normal. Dr. GUI can even provide a quick example. (Of course, there are other solutions. As long as the response contains the correct headers and characters in the correct order, the browser will interpret the response so that the response works properly. You can leverage string concatenation to generate the response if needed.)
Below In his example, Dr. GUI uses JScript for client-side code and VBScript (Microsoft Visual Basic Scripting Edition) in an ASP page. The strategy adopted is this: the XML file contains only data (and a reference to the style sheet). The XSL file contains all the HTML formatting necessary to form a complete page (including scripts in place). An ASP page essentially consists of a few calls to create an XMLHTTP object, load an XML file into the object (using an implicit style sheet), and write the resulting HTML to the response stream.
To try this method, follow the simple steps shown below. Use Notepad to copy and paste and save all files in c:inetpubwwwroot.
1.
Create a file named books.xml that contains the following XML text. Make a note of the reference to the books.xsl style sheet. Other than that, this file only contains data.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="books.xsl" ?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
</book>
</catalog>
2.
Then, create a file named books.xsl that contains the following code. This file is the style sheet referenced by the books.xml created in step 1. It contains the information necessary to correctly format the above XML file into an HTML page and even include scripts on the HTML page.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl=" http://www.w3.org/1999/XSL/Transform ">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Test</title>
</head>
<script language="JScript">
function Test_OnLoad()
{
var objXML = new ActiveXObject("MSXML2.DOMDocument");
objXML.async=false;
objXML.load("books.xml");
alert(objXML.xml);
objXML=null;
}
</script>
<body onload="Test_OnLoad()">
<table border="1">
<xsl:for-each select="catalog/book">
<tr>
<td>
<xsl:value-of select="author"/>
</td>
<td>
<xsl:value-of select="title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
3.
Finally, create a file named books.asp that contains the following code. This file is used only to create the XMLHTTP object, load the XML file for it, and write the resulting HTML to the response stream.
<%
dim sxh
Response.contentType = "text/xml"
set sxh = Server.CreateObject("MSXML2.XMLHTTP.3.0")
sxh.open "GET"," http://localhost/books.xml",false
sxh.send
Response.write sxh.responsetext
%>
4.
Open your browser and navigate to http://localhost/books.asp .
5.
You see an alert showing the data containing books.xml. This is done by a script in the XSL file. You'll also see the data neatly formatted in a table on the web page. This is done by XSL code.