Entry
XML: How can I use the DOM-method "transformNodeToObject"?
Sep 16th, 2003 08:37
Knud van Eeden, Katrin Weineck,
----------------------------------------------------------------------
--- Knud van Eeden - Saturday 22 September 2001 - 01:42 --------------
When you run the following JavaScript program (you must have Microsoft
Internet Explorer v5.0 or higher installed (I tested it with Microsoft
Internet Explorer v6.0 and that worked OK), and using that as your
browser), you will first see the generated HTML source code, which is a
result of the applied XSL stylesheet. This by using
transformNodeToObject().
Then you will see also the output result of this HTML source code, by
using
transformNode().
---
---
1. Save the following text, via copy and paste, as the plain text file
book.htm:
<HTML>
<BODY onLoad=' PROCXmLXsLView( "book.xml", "book.xsl" );'>
</BODY>
</HTML>
<SCRIPT LANGUAGE = "JavaScript">
<!--
<!-- library: view XML using XSL (works correctly in Microsoft
Internet
Explorer v6.0) [kn, ri, sa, 22-09-2001 01:21:09] -->
function PROCXmLXsLView( filenamexmlS, filenamexslS ) {
var xmldoc = null;
var xsldoc = null;
var resultdoc = null;
//
xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.async = false;
xmldoc.load( filenamexmlS );
alert( "input: XML file = " + xmldoc.xml );
//
xsldoc = new ActiveXObject("Microsoft.XMLDOM");
xsldoc.async = false;
xsldoc.load( filenamexslS );
alert( "input: XSL file = " + xsldoc.xml );
//
resultdoc = new ActiveXObject("Microsoft.XMLDOM");
resultdoc.async = false;
resultdoc.validateOnParse = true;
//
xmldoc.transformNodeToObject( xsldoc, resultdoc );
alert( "output: source code = " + resultdoc.xml );
//
document.write( "output: view in browser = <BR>" +
xmldoc.transformNode
( xsldoc ) );
}
// -->
</SCRIPT>
---
2. Supplied as an example with this file 'book.htm' are the following
3 files:
2.1 book.xml
2.2 book.dtd
2.3 book.xsl
---
2.1 this is the file 'book.xml' (save this text as a plain text file
in the
same directory as the file book.htm)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE MYBOOKS SYSTEM "book.dtd">
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<!-- library: (filenamemacro=book.xml) -->
<MYBOOKS>
<BOOK>
<AUTHOR>
Holzner, Steve
</AUTHOR>
<TITLE>
Inside XML
</TITLE>
</BOOK>
<BOOK>
<AUTHOR>
Knuth, Donald
</AUTHOR>
<TITLE>
the art of computer programming
</TITLE>
</BOOK>
</MYBOOKS>
---
2.2 this is the file 'book.dtd' (save this text as a plain text file
in the
same directory as the file book.htm)
<!ELEMENT MYBOOKS (BOOK+)>
<!ELEMENT BOOK (AUTHOR,TITLE)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
---
2.3 this is the file 'book.xsl' (save this text as a plain text file
in the
same directory as the file book.htm)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="MYBOOKS">
<HTML>
<HEAD>
<TITLE>
My book database
</TITLE>
</HEAD>
<BODY>
<H3> Overview book database </H3>
<BR/>
<BR/>
<HR/>
<HR/>
<xsl:apply-templates/>
<HR/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="BOOK">
<FONT SIZE="3" COLOR="blue">
<BR/> book:
<xsl:apply-templates/>
</FONT>
<HR/>
</xsl:template>
<xsl:template match="AUTHOR">
<BR/> author=
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="TITLE">
<BR/> title=
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
---
3. Then load this file 'book.htm' in your browser
(e.g. type
c:\temp\book.htm
in the URL field of your browser, given that you stored
all 4 files in the c:\temp directory, on your disk.
(Make sure this 3 files book.xml, book.dtd, book.xsl are thus
be stored in that same directory as book.htm)
---
4. As a result you will see:
---
4.1 first the generated HTML source code (using method
transformNodeToObject())
+------------------------------------+
|<HTML> |
| <HEAD> |
| <TITLE> |
| My book database |
| </TITLE> |
| </HEAD> |
| <BODY> |
| <H3> Overview book database </H3> |
| <BR/> |
| <BR/> |
| <HR/> |
| <HR/> |
| <FONT SIZE="3" COLOR="blue"> |
| <BR/> book: |
| <BR/> author= |
| Holzner, Steve |
| <BR/> title= |
| Inside XML |
| </FONT> |
| <HR/> |
| <FONT SIZE="3" COLOR="blue"> |
| <BR/> book: |
| <BR/> author= |
| Knuth, Donald |
| <BR/> title= |
| the art of computer programming|
| </FONT> |
| <HR/> |
| <HR/> |
| </BODY> |
|</HTML> |
| |
+------------------------------------+
---
4.2 then the result of viewing this generated HTML source code in
your browser (using method transformNode())
+------------------------------------------+
|Overview book database |
| |
| |
| |
|------------------------------------------|
| |
|------------------------------------------|
| |
|book: |
|author= Holzner, Steve |
|title= Inside XML |
|------------------------------------------|
| |
|book: |
|author= Knuth, Donald |
|title= the art of computer programming |
|------------------------------------------|
| |
|------------------------------------------|
+------------------------------------------+
---
[Internet: see also:
http://www.faqts.com/knowledge_base/view.phtml/aid/6640]
http://www.faqts.com/knowledge_base/view.phtml/aid/24269/fid/613
--- Knud van Eeden - 22 September 2001 - 01:45 -----------------------
---------------------------------------------------------------------