473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

XML Beginner for Client-side server

I am doing a simple client-side example using DOM in JScript. When the
user enter info and click on the submit button, it will then print the
value at the bottom of the page. However, I don't know what's wrong
that the entered values are not printed out. Can anyone please help
me??? Any reply is appreciated. My code is as follow, you can copy and
run it yourself.

File name: bookClient.htm

<html>
<head>
<title>Wrox Press book data entry page</title>
<body onload="initializeBook()" >
<h1>Wrox Press book data entry page</h1>
<h3>Book information:</h3>
<table>
<tr>
<td>Title:</td>
<td><input id="txtTitle"></input></td>
</tr>
<tr>
<td>Publisher:</td><td><input id="txtPublisher"></input></td>
</tr>
<tr>
<td>Published Date:</td><td><input id="txtPubDate"></input></td>
</tr>
<tr>
<td>Abstract:</td><td><input id="txtAbstract"></input></td>
</tr>
<tr>
<td>Pages:</td><td><input id="txtPages"></input></td>
</tr>
<tr>
<td>ISBN:</td><td><input id="txtISBN"></input></td>
</tr>
<tr>
<td>Price:</td><td><input id="txtPrice"></input></td>
</tr>
</table>
<input id="btnUpdate" type="button" value="Update book info"
onclick="updateBookInfo()"></input>
<h3>Authors:</h3>
<table>
<tr>
<td>Author:</td><td><input id="txtAuthor"></input></td>
</tr>
</table>
<input id="btnAddAuthor" type="button" value="Add author"
onclick="addAuthor()"></input>

<h3>Categories:</h3>
<table>
<tr>
<td>Category:</td><td><input id="txtCategory"></input></td>
</tr>
</table>
<input id="btnAddCategory" type="button" value="Add Category"
onclick="addCategory()"></input>
<xml id="docBook">
<book>
</book>
</xml>
<script>
var docBook;
function initializeBook()
{

docBook = document.all("docBook").XMLDocument;
docBook.async = "false";
renderElements();
}

function createOrReplaceElement(sElementName, sElementValue,
elementParent)
{
var elementItem;
var textValue;
var nodelistOldItem;

elementItem = docBook.createElement(sElementName);
textValue = docBook.createTextNode(sElementValue);
elementItem.appendChild(textValue);

nodelistOldItem = elementParent.getElementsByTagName(sElementName);
if (nodelistOldItem.length > 0)
{
elementParent.replaceChild(elementItem, nodelistOldItem.item(0));
}
else
{
elementParent.appendChild(elementItem);
}
}
function updateBookInfo()
{
createOrReplaceElement("Title", txtTitle.value,
docBook.documentElement);
createOrReplaceElement("Publisher", txtPublisher.value,
docBook.documentElement);
createOrReplaceElement("PubDate", txtPubDate.value,
docBook.documentElement);
createOrReplaceElement("Abstract", txtAbstract.value,
docBook.documentElement);
createOrReplaceElement("Pages", txtPages.value,
docBook.documentElement);
createOrReplaceElement("ISBN", txtISBN.value,
docBook.documentElement);
createOrReplaceElement("Price", txtPrice.value,
docBook.documentElement);

renderElements();
}
function addAuthor()
{
var elementAuthor;
var textAuthor;
var nodelistAuthors;
var elementAuthors;
elementAuthor = docBook.createElement("author");
textAuthor = docBook.createElement(txtAuthor.value);
elementAuthor.appendChild(textAuthor);
nodelistAuthors = docBook.getElementsByTagName("authors");
if (nodelistAuthors.length == 0)
{
elementAuthors = docBook.createElement("authors");
docBook.documentElement.appendChild(elementAuthors );
}
else
{
elementAuthors = nodelistAuthors.item(0);
}
elementAuthors.appendChild(elementAuthor);
renderElements();
}
function addCategory()
{
var elementCategory;
var textCategory;
var nodelistRecSubjCategories;
var elementRecSubjCategories;
elementCategory = docBook.createElement("category");
textCategory = docBook.createElement(txtCategory.value);
elementCategory.appendChild(textCategory);
nodelistRecSubjCategories =
docBook.getElementsByTagName("recSubjCategories");
if (nodelistRecSubjCategories.length == 0)
{
elementRecSubjCategories =
docBook.createElement("recSubjCategories");
docBook.documentElement.appendChild(elementRecSubj Categories);
}
else
{
elementRecSubjCategories = nodelistRecSubjCategories.item(0);
}
elementRecSubjCategories.appendChild(elementCatego ry);
renderElements();
}
function renderElements()
{

document.all("divRawXML").innerText = docBook.xml;
bookInfo.innerHTML = docBook.transformNode(bookXSL.documentElement);
authorTable.innerHTML =
docBook.transformNode(authorXSL.documentElement);
categoryTable.innerHTML =
docBook.transformNode(categoryXSL.documentElement) ;
}
</script>
<xml id="bookXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:choose>
<xsl:when test="/book/title[. $ne$ '']">
<table border="0" cellpadding="1">
<tr>
<td>Title:</td>
<td><xsl:value-of select="/book/title"/></td>
</tr>
<tr>
<td>Publisher:</td>
<td><xsl:value-of select="/book/publisher"/></td>
</tr>
<tr>
<td>Published Date:</td>
<td><xsl:value-of select="/book/pubDate"/></td>
</tr>
<tr>
<td>Abstract:</td>
<td><xsl:value-of select="/book/abstract"/></td>
</tr>
<tr>
<td>Pages:</td>
<td><xsl:value-of select="/book/pages"/></td>
</tr>
<tr>
<td>ISBN:</td>
<td><xsl:value-of select="/book/isbn"/></td>
</tr>
<tr>
<td>Price:</td>
<td><xsl:value-of select="/book/price"/></td>
</tr>
</table>
</xsl:when>
<xsl:otherwise>
<p>Book Information not yet specified.</p>
</xsl:otherwise>
</xsl:choose>
</div>
</xml>
<xml id="authorXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<table border="0" cellpadding="1">
<tr>
<td><strong>Authors</strong></td>
</tr>
<xsl:for-each select="/book/authors/author">
<tr>
<td><xsl:value-of select="text()"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</xml>
<xml id="categoryXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/category">
<table border="0" cellpadding="1">
<tr>
<td><strong>Categories</strong></td>
</tr>
<xsl:for-each select="category">
<tr>
<td><xsl:value-of select="category"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</div>
</xml>
<hr>
<h2>Book information</h2>
<p><div id="bookInfo"></div></p>
<p><div id="authorTable"></div></p>
<p><div id="categoryTable"></div></p>
</hr>
The text expression of the current contents of the DOM tree is:
<pre><div id="divRawXML"></div></pre>
</body>
</head>
</html>
Jul 20 '05 #1
1 1691
Browsers render HTML as a stream, rather than as a parse tree. Try to
put your script at the beginning of the document, rather than at the end.

Regards,
Oleg
Renee wrote:
I am doing a simple client-side example using DOM in JScript. When the
user enter info and click on the submit button, it will then print the
value at the bottom of the page. However, I don't know what's wrong
that the entered values are not printed out. Can anyone please help
me??? Any reply is appreciated. My code is as follow, you can copy and
run it yourself.

File name: bookClient.htm

<html>
<head>
<title>Wrox Press book data entry page</title>
<body onload="initializeBook()" >
<h1>Wrox Press book data entry page</h1>
<h3>Book information:</h3>
<table>
<tr>
<td>Title:</td>
<td><input id="txtTitle"></input></td>
</tr>
<tr>
<td>Publisher:</td><td><input id="txtPublisher"></input></td>
</tr>
<tr>
<td>Published Date:</td><td><input id="txtPubDate"></input></td>
</tr>
<tr>
<td>Abstract:</td><td><input id="txtAbstract"></input></td>
</tr>
<tr>
<td>Pages:</td><td><input id="txtPages"></input></td>
</tr>
<tr>
<td>ISBN:</td><td><input id="txtISBN"></input></td>
</tr>
<tr>
<td>Price:</td><td><input id="txtPrice"></input></td>
</tr>
</table>
<input id="btnUpdate" type="button" value="Update book info"
onclick="updateBookInfo()"></input>
<h3>Authors:</h3>
<table>
<tr>
<td>Author:</td><td><input id="txtAuthor"></input></td>
</tr>
</table>
<input id="btnAddAuthor" type="button" value="Add author"
onclick="addAuthor()"></input>

<h3>Categories:</h3>
<table>
<tr>
<td>Category:</td><td><input id="txtCategory"></input></td>
</tr>
</table>
<input id="btnAddCategory" type="button" value="Add Category"
onclick="addCategory()"></input>
<xml id="docBook">
<book>
</book>
</xml>
<script>
var docBook;
function initializeBook()
{

docBook = document.all("docBook").XMLDocument;
docBook.async = "false";
renderElements();
}

function createOrReplaceElement(sElementName, sElementValue,
elementParent)
{
var elementItem;
var textValue;
var nodelistOldItem;

elementItem = docBook.createElement(sElementName);
textValue = docBook.createTextNode(sElementValue);
elementItem.appendChild(textValue);

nodelistOldItem = elementParent.getElementsByTagName(sElementName);
if (nodelistOldItem.length > 0)
{
elementParent.replaceChild(elementItem, nodelistOldItem.item(0));
}
else
{
elementParent.appendChild(elementItem);
}
}
function updateBookInfo()
{
createOrReplaceElement("Title", txtTitle.value,
docBook.documentElement);
createOrReplaceElement("Publisher", txtPublisher.value,
docBook.documentElement);
createOrReplaceElement("PubDate", txtPubDate.value,
docBook.documentElement);
createOrReplaceElement("Abstract", txtAbstract.value,
docBook.documentElement);
createOrReplaceElement("Pages", txtPages.value,
docBook.documentElement);
createOrReplaceElement("ISBN", txtISBN.value,
docBook.documentElement);
createOrReplaceElement("Price", txtPrice.value,
docBook.documentElement);

renderElements();
}
function addAuthor()
{
var elementAuthor;
var textAuthor;
var nodelistAuthors;
var elementAuthors;
elementAuthor = docBook.createElement("author");
textAuthor = docBook.createElement(txtAuthor.value);
elementAuthor.appendChild(textAuthor);
nodelistAuthors = docBook.getElementsByTagName("authors");
if (nodelistAuthors.length == 0)
{
elementAuthors = docBook.createElement("authors");
docBook.documentElement.appendChild(elementAuthors );
}
else
{
elementAuthors = nodelistAuthors.item(0);
}
elementAuthors.appendChild(elementAuthor);
renderElements();
}
function addCategory()
{
var elementCategory;
var textCategory;
var nodelistRecSubjCategories;
var elementRecSubjCategories;
elementCategory = docBook.createElement("category");
textCategory = docBook.createElement(txtCategory.value);
elementCategory.appendChild(textCategory);
nodelistRecSubjCategories =
docBook.getElementsByTagName("recSubjCategories");
if (nodelistRecSubjCategories.length == 0)
{
elementRecSubjCategories =
docBook.createElement("recSubjCategories");
docBook.documentElement.appendChild(elementRecSubj Categories);
}
else
{
elementRecSubjCategories = nodelistRecSubjCategories.item(0);
}
elementRecSubjCategories.appendChild(elementCatego ry);
renderElements();
}
function renderElements()
{

document.all("divRawXML").innerText = docBook.xml;
bookInfo.innerHTML = docBook.transformNode(bookXSL.documentElement);
authorTable.innerHTML =
docBook.transformNode(authorXSL.documentElement);
categoryTable.innerHTML =
docBook.transformNode(categoryXSL.documentElement) ;
}
</script>
<xml id="bookXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:choose>
<xsl:when test="/book/title[. $ne$ '']">
<table border="0" cellpadding="1">
<tr>
<td>Title:</td>
<td><xsl:value-of select="/book/title"/></td>
</tr>
<tr>
<td>Publisher:</td>
<td><xsl:value-of select="/book/publisher"/></td>
</tr>
<tr>
<td>Published Date:</td>
<td><xsl:value-of select="/book/pubDate"/></td>
</tr>
<tr>
<td>Abstract:</td>
<td><xsl:value-of select="/book/abstract"/></td>
</tr>
<tr>
<td>Pages:</td>
<td><xsl:value-of select="/book/pages"/></td>
</tr>
<tr>
<td>ISBN:</td>
<td><xsl:value-of select="/book/isbn"/></td>
</tr>
<tr>
<td>Price:</td>
<td><xsl:value-of select="/book/price"/></td>
</tr>
</table>
</xsl:when>
<xsl:otherwise>
<p>Book Information not yet specified.</p>
</xsl:otherwise>
</xsl:choose>
</div>
</xml>
<xml id="authorXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<table border="0" cellpadding="1">
<tr>
<td><strong>Authors</strong></td>
</tr>
<xsl:for-each select="/book/authors/author">
<tr>
<td><xsl:value-of select="text()"/></td>
</tr>
</xsl:for-each>
</table>
</div>
</xml>
<xml id="categoryXSL">
<div xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/category">
<table border="0" cellpadding="1">
<tr>
<td><strong>Categories</strong></td>
</tr>
<xsl:for-each select="category">
<tr>
<td><xsl:value-of select="category"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</div>
</xml>
<hr>
<h2>Book information</h2>
<p><div id="bookInfo"></div></p>
<p><div id="authorTable"></div></p>
<p><div id="categoryTable"></div></p>
</hr>
The text expression of the current contents of the DOM tree is:
<pre><div id="divRawXML"></div></pre>
</body>
</head>
</html>


Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter | last post by:
I have a small custom 404 page in PHP. When this page is triggered I'd like some information about the requesting client, including its reverse IP. I would like the 404 page to be returned to the...
13
by: ^MisterJingo^ | last post by:
I've been reading up on interfaces, one example I came across showed that you can hide a method implemented from an interface from the class which implements it. To use it, you must cast to the...
18
by: mitchellpal | last post by:
Hi guys, am learning c as a beginner language and am finding it rough especially with pointers and data files. What do you think, am i being too pessimistic or thats how it happens for a beginner?...
5
by: Yourko | last post by:
Hi there! I`me currently trying to write some simple programs in C. For one such program i created globals.h file. In that file i defined a structure of type _client, and a pointer of that type: ...
3
by: Yourko | last post by:
Hello! I ame working on a hobby-project - a simple TCP/IP server. Its purpose (for now) is to act like a chat program: send all received data from one connected client to all others. First step is...
15
by: RAM | last post by:
Hello, I graduated computer science faculty and decided to became a programmer. Please help me to make a decision: Java or Microsoft .NET? What is the future of Java? Thanks! /RAM/
0
by: JJ | last post by:
I've been reading a lot of articles about custom entity vs strongly typed dataset and decided go with the custom entity model. Just curious about how to design classes. For the simplicity of...
6
by: harky | last post by:
Hi I need some serious help!! I am a beginner with very little knowledge of access I am looking to get a database to tell me who has not payed their bill, they have 30 days to pay it. if...
0
by: smilyface | last post by:
Hello, I'm a beginner with python and I have a problem. I'm using Python 2.5 with PyWin32 on Windows Vista. I try to run this script Office2PDF
25
by: doznot | last post by:
Let's say you want to use Moodle to teach an introductory class in PHP programming. Some of the students have little or no computer experience. In addition to background reading and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.