今天就是在学习XML DOM编程过程中,出现了一个错误,浏览器总是告诉我有一个参数无效,后来经过认真研究终于找到了。
先把代码给大家看看吧。
1
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
2 < html >
3 < head >
4 < title ></ title >
5 < meta name ="GENERATOR" content ="Microsoft Visual Studio .NET 7.1" >
6 < meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
7 </ head >
8 < body onload ="initializeBook();" >
9 < h1 > Wrox Press </ h1 >
10 < h3 > Book Information </ h3 >
11 < TABLE BORDER ="0" CELLSPACING ="0" CELLPADDING ="0" >
12 < TR >
13 < TD > Title: </ TD >< TD >< input id ="txtTitle" ></ TD >
14 </ TR >
15 < TR >
16 < TD > Publisher: </ TD >< TD >< input id ="txtPublisher" ></ TD >
17 </ TR >
18 < TR >
19 < TD > Published Date: </ TD >< TD >< input id ="txtPubDate" ></ TD >
20 </ TR >
21 < TR >
22 < TD > Abstract: </ TD >< TD >< input id ="txtAbstract" ></ TD >
23 </ TR >
24 < TR >
25 < TD > Pages </ TD >< TD >< input id ="txtPages" ></ TD >
26 </ TR >
27 < TR >
28 < TD > ISBN: </ TD >< TD >< input id ="txtISBN" ></ TD >
29 </ TR >
30 < TR >
31 < TD > Price: </ TD >< TD >< input id ="txtPrice" ></ TD >
32 </ TR >
33
34 </ TABLE >
35
36 < input type ="button" id ="btnUpdate" value ="Update Book Info" onclick ="updateBookInfo();" />
37
38 < h3 > Author: </ h3 >
39 < table >
40 < tr >
41 < td > Author: </ td >< td >< input id ="txtAuthor" ></ td >
42 </ tr >
43 </ table >
44 < input type ="button" id ="btnAddAuthor" value ="Add Author" onclick ="addAuthor();" />
45 < h3 > Catagories: </ h3 >
46 < table >
47 < tr >
48 < td > Category: </ td >< td >< input id ="txtCategory" ></ td >
49 </ tr >
50 </ table >
51 < input type ="button" id ="btnAddCategory" value ="Add category" onclick ="addCategory();" />
52
53 < XML id =docBook >
54 < Book >
55 </ Book >
56 </ XML >
57
58 < script >
59 var docBook;
60 function initializeBook()
61 {
62 docBook=document.all("docBook").XMLDocument;
63 docBook.async=false;
64 renderElements();
65 }
66
67 function createOrReplaceElement(sElementName,sElementValue,elementParent)
68 {
69 var elementItem;
70 var textValue;
71 var nodelistOldItem;
72
73 elementItem=docBook.createElement(sElementName);
74 textValue=docBook.createTextNode(sElementValue);
75 elementItem.appendChild(textValue);
76
77 nodelistOldItem=elementParent.getElementsByTagName(sElementName);
78 if (nodelistOldItem.length>0)
79 {
80 elementParent.replaceChild(elementItem,nodelistOldItem.item(0));
81 }
82 else
83 {
84 elementParent.appendChild(elementItem);
85 }
86
87 }
88
89 function updateBookInfo()
90 {
91 createOrReplaceElement("Title",txtTitle.value,docBook.documentElement);
92 createOrReplaceElement("Publisher",txtPublisher.value,docBook.documentElement);
93 createOrReplaceElement("PubDate",txtPubDate.value,docBook.documentElement);
94 createOrReplaceElement("Abstract",txtAbstract.value,docBook.documentElement);
95 createOrReplaceElement("Pages",txtPages.value,docBook.documentElement);
96 createOrReplaceElement("ISBN",txtISBN.value,docBook.documentElement);
97 createOrReplaceElement("Price",txtPubDate.value,docBook.documentElement);
98
99 renderElements();
100 }
101
102 function addAuthor()
103 {
104 var elementAuthor;
105 var textAuthor;
106 var nodelistAuthors;
107 var elementAuthors;
108
109 elementAuthor=docBook.createElement("Author");
110 textAuthor=docBook.createTextNode(txtAuthor.value);
111 elementAuthor.appendChild(textAuthor);
112 nodelistAuthors=docBook.getElementsByTagName("Authors");
113 if(nodelistAuthors.length==0)
114 {
115 elementAuthors=docBook.createElement("Authors");
116 docBook.documentElement.appendChild(elementAuthors);
117 }
118 else
119 {
120 elementAuthors=nodelistAuthors.item(0);
121 }
122 elementAuthors.appendChild(elementAuthor);
123 renderElements();
124 }
125
126 function addCategory()
127 {
128 var elementCategory;
129 var textCategory;
130 var nodelistRecSubjCategories;
131 var elementRecSubjCategories;
132
133 elementCategory=docBook.createElement("Category");
134 textCategory=docBook.createTextNode(txtCategory.value);
135 elementCategory.appendChild(textCategory);
136 nodelistRecSubjCategories=docBook.getElementsByTagName("RecSubjCategories");
137
138 if(nodelistRecSubjCategories.length==0)
139 {
140 elementRecSubjCategories=docBook.createElement("RecSubjCategories");
141 docBook.documentElement.appendChild(elementRecSubjCategories);
142 }
143 else
144 {
145 elementRecSubjCategories=nodelistRecSubjCategories.item(0);
146 }
147
148 elementRecSubjCategories.appendChild(elementCategory);
149
150 renderElements();
151 }
152
153 function renderElements()
154 {
155 document.all("divRawXML").innerText=docBook.xml;
156 bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
157 authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
158 categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);
159
160 }
161
162
163 </ script >
164
165
166 < XML id =bookXSL >
167 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
168 < xsl:choose >
169 < xsl:when test ="/Book/Title[. $ne$ '']" >
170 < table border ="0" cellpadding ="1" >
171 < tr >
172 < td > Title: </ td >< td >< xsl:value-of select ="/Book/Title" /></ td >
173 </ tr >
174 < tr >
175 < td > Publisher: </ td >< td >< xsl:value-of select ="/Book/Publisher" /></ td >
176 </ tr >
177 < tr >
178 < td > Published Date: </ td >< td >< xsl:value-of select ="/Book/PubDate" /></ td >
179 </ tr >
180 < tr >
181 < td > Abstract: </ td >< td >< xsl:value-of select ="/Book/Abstract" /></ td >
182 </ tr >
183 < tr >
184 < td > Pages: </ td >< td >< xsl:value-of select ="/Book/Pages" /></ td >
185 </ tr >
186 < tr >
187 < td > ISBN: </ td >< td >< xsl:value-of select ="/Book/ISBN" /></ td >
188 </ tr >
189 < tr >
190 < td > Price: </ td >< td >< xsl:value-of select ="/Book/Price" /></ td >
191 </ tr >
192 </ table >
193 </ xsl:when >
194 < xsl:otherwise >
195 Book Information not yet specified.
196 </ xsl:otherwise >
197 </ xsl:choose >
198 </ DIV >
199
200 </ XML >
201
202 < XML id =authorXSL >
203 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
204 < table border ="0" cellpadding ="1" >
205 < tr >
206 < td >< strong > Authors </ strong ></ td >
207 </ tr >
208 < xsl:for-each select ="/Book/Authors/Author" >
209 < tr >
210 < td >< xsl:value-of select ="text()" /></ td >
211 </ tr >
212 </ xsl:for-each >
213 </ table >
214 </ DIV >
215 </ XML >
216
217 < XML id =categoryXSL >
218 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
219 < table border ="0" cellpadding ="1" >
220 < tr >
221 < td >< strong > Categories </ strong ></ td >
222 </ tr >
223 < xsl:for-each select ="/Book/RecSubjCategories/Category" >
224 < tr >
225 < td >< xsl:value-of select ="text()" /></ td >
226 </ tr >
227 </ xsl:for-each >
228 </ table >
229 </ DIV >
230 </ XML >
231
232 < hr />
233 < h2 > Book Information </ h2 >
234 < div id ="bookInfo" ></ div >
235 < div id ="authorTable" ></ div >
236 < div id ="categoryTable" ></ div >
237 < hr />
238 The Text expression of the current contents of the DOM tree is:
239 < DIV id ="divRawXML" ></ DIV >
240
241
242 </ body >
243 </ html >
244
245
2 < html >
3 < head >
4 < title ></ title >
5 < meta name ="GENERATOR" content ="Microsoft Visual Studio .NET 7.1" >
6 < meta name ="vs_targetSchema" content ="http://schemas.microsoft.com/intellisense/ie5" >
7 </ head >
8 < body onload ="initializeBook();" >
9 < h1 > Wrox Press </ h1 >
10 < h3 > Book Information </ h3 >
11 < TABLE BORDER ="0" CELLSPACING ="0" CELLPADDING ="0" >
12 < TR >
13 < TD > Title: </ TD >< TD >< input id ="txtTitle" ></ TD >
14 </ TR >
15 < TR >
16 < TD > Publisher: </ TD >< TD >< input id ="txtPublisher" ></ TD >
17 </ TR >
18 < TR >
19 < TD > Published Date: </ TD >< TD >< input id ="txtPubDate" ></ TD >
20 </ TR >
21 < TR >
22 < TD > Abstract: </ TD >< TD >< input id ="txtAbstract" ></ TD >
23 </ TR >
24 < TR >
25 < TD > Pages </ TD >< TD >< input id ="txtPages" ></ TD >
26 </ TR >
27 < TR >
28 < TD > ISBN: </ TD >< TD >< input id ="txtISBN" ></ TD >
29 </ TR >
30 < TR >
31 < TD > Price: </ TD >< TD >< input id ="txtPrice" ></ TD >
32 </ TR >
33
34 </ TABLE >
35
36 < input type ="button" id ="btnUpdate" value ="Update Book Info" onclick ="updateBookInfo();" />
37
38 < h3 > Author: </ h3 >
39 < table >
40 < tr >
41 < td > Author: </ td >< td >< input id ="txtAuthor" ></ td >
42 </ tr >
43 </ table >
44 < input type ="button" id ="btnAddAuthor" value ="Add Author" onclick ="addAuthor();" />
45 < h3 > Catagories: </ h3 >
46 < table >
47 < tr >
48 < td > Category: </ td >< td >< input id ="txtCategory" ></ td >
49 </ tr >
50 </ table >
51 < input type ="button" id ="btnAddCategory" value ="Add category" onclick ="addCategory();" />
52
53 < XML id =docBook >
54 < Book >
55 </ Book >
56 </ XML >
57
58 < script >
59 var docBook;
60 function initializeBook()
61 {
62 docBook=document.all("docBook").XMLDocument;
63 docBook.async=false;
64 renderElements();
65 }
66
67 function createOrReplaceElement(sElementName,sElementValue,elementParent)
68 {
69 var elementItem;
70 var textValue;
71 var nodelistOldItem;
72
73 elementItem=docBook.createElement(sElementName);
74 textValue=docBook.createTextNode(sElementValue);
75 elementItem.appendChild(textValue);
76
77 nodelistOldItem=elementParent.getElementsByTagName(sElementName);
78 if (nodelistOldItem.length>0)
79 {
80 elementParent.replaceChild(elementItem,nodelistOldItem.item(0));
81 }
82 else
83 {
84 elementParent.appendChild(elementItem);
85 }
86
87 }
88
89 function updateBookInfo()
90 {
91 createOrReplaceElement("Title",txtTitle.value,docBook.documentElement);
92 createOrReplaceElement("Publisher",txtPublisher.value,docBook.documentElement);
93 createOrReplaceElement("PubDate",txtPubDate.value,docBook.documentElement);
94 createOrReplaceElement("Abstract",txtAbstract.value,docBook.documentElement);
95 createOrReplaceElement("Pages",txtPages.value,docBook.documentElement);
96 createOrReplaceElement("ISBN",txtISBN.value,docBook.documentElement);
97 createOrReplaceElement("Price",txtPubDate.value,docBook.documentElement);
98
99 renderElements();
100 }
101
102 function addAuthor()
103 {
104 var elementAuthor;
105 var textAuthor;
106 var nodelistAuthors;
107 var elementAuthors;
108
109 elementAuthor=docBook.createElement("Author");
110 textAuthor=docBook.createTextNode(txtAuthor.value);
111 elementAuthor.appendChild(textAuthor);
112 nodelistAuthors=docBook.getElementsByTagName("Authors");
113 if(nodelistAuthors.length==0)
114 {
115 elementAuthors=docBook.createElement("Authors");
116 docBook.documentElement.appendChild(elementAuthors);
117 }
118 else
119 {
120 elementAuthors=nodelistAuthors.item(0);
121 }
122 elementAuthors.appendChild(elementAuthor);
123 renderElements();
124 }
125
126 function addCategory()
127 {
128 var elementCategory;
129 var textCategory;
130 var nodelistRecSubjCategories;
131 var elementRecSubjCategories;
132
133 elementCategory=docBook.createElement("Category");
134 textCategory=docBook.createTextNode(txtCategory.value);
135 elementCategory.appendChild(textCategory);
136 nodelistRecSubjCategories=docBook.getElementsByTagName("RecSubjCategories");
137
138 if(nodelistRecSubjCategories.length==0)
139 {
140 elementRecSubjCategories=docBook.createElement("RecSubjCategories");
141 docBook.documentElement.appendChild(elementRecSubjCategories);
142 }
143 else
144 {
145 elementRecSubjCategories=nodelistRecSubjCategories.item(0);
146 }
147
148 elementRecSubjCategories.appendChild(elementCategory);
149
150 renderElements();
151 }
152
153 function renderElements()
154 {
155 document.all("divRawXML").innerText=docBook.xml;
156 bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
157 authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
158 categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);
159
160 }
161
162
163 </ script >
164
165
166 < XML id =bookXSL >
167 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
168 < xsl:choose >
169 < xsl:when test ="/Book/Title[. $ne$ '']" >
170 < table border ="0" cellpadding ="1" >
171 < tr >
172 < td > Title: </ td >< td >< xsl:value-of select ="/Book/Title" /></ td >
173 </ tr >
174 < tr >
175 < td > Publisher: </ td >< td >< xsl:value-of select ="/Book/Publisher" /></ td >
176 </ tr >
177 < tr >
178 < td > Published Date: </ td >< td >< xsl:value-of select ="/Book/PubDate" /></ td >
179 </ tr >
180 < tr >
181 < td > Abstract: </ td >< td >< xsl:value-of select ="/Book/Abstract" /></ td >
182 </ tr >
183 < tr >
184 < td > Pages: </ td >< td >< xsl:value-of select ="/Book/Pages" /></ td >
185 </ tr >
186 < tr >
187 < td > ISBN: </ td >< td >< xsl:value-of select ="/Book/ISBN" /></ td >
188 </ tr >
189 < tr >
190 < td > Price: </ td >< td >< xsl:value-of select ="/Book/Price" /></ td >
191 </ tr >
192 </ table >
193 </ xsl:when >
194 < xsl:otherwise >
195 Book Information not yet specified.
196 </ xsl:otherwise >
197 </ xsl:choose >
198 </ DIV >
199
200 </ XML >
201
202 < XML id =authorXSL >
203 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
204 < table border ="0" cellpadding ="1" >
205 < tr >
206 < td >< strong > Authors </ strong ></ td >
207 </ tr >
208 < xsl:for-each select ="/Book/Authors/Author" >
209 < tr >
210 < td >< xsl:value-of select ="text()" /></ td >
211 </ tr >
212 </ xsl:for-each >
213 </ table >
214 </ DIV >
215 </ XML >
216
217 < XML id =categoryXSL >
218 < DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
219 < table border ="0" cellpadding ="1" >
220 < tr >
221 < td >< strong > Categories </ strong ></ td >
222 </ tr >
223 < xsl:for-each select ="/Book/RecSubjCategories/Category" >
224 < tr >
225 < td >< xsl:value-of select ="text()" /></ td >
226 </ tr >
227 </ xsl:for-each >
228 </ table >
229 </ DIV >
230 </ XML >
231
232 < hr />
233 < h2 > Book Information </ h2 >
234 < div id ="bookInfo" ></ div >
235 < div id ="authorTable" ></ div >
236 < div id ="categoryTable" ></ div >
237 < hr />
238 The Text expression of the current contents of the DOM tree is:
239 < DIV id ="divRawXML" ></ DIV >
240
241
242 </ body >
243 </ html >
244
245
出现问题的代码是这一行:
1
function
renderElements()
2 {
3 document.all("divRawXML").innerText=docBook.xml;
4 bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
5 authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
6 categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);//出现问题的代码
7
8 }
可见,是categoryXSL.documentElement这个参数出了问题,也就是XSL出了问题,但是笔者查了半天也没搞明白,最后终于找到了代码的问题,原来出现错误的代码是这样的:
2 {
3 document.all("divRawXML").innerText=docBook.xml;
4 bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
5 authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
6 categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);//出现问题的代码
7
8 }
1
<
XML
id
=categoryXSL>
2 <DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
3 < table border ="0" cellpadding ="1" >
4 < tr >
5 < td >< strong > Categories </ strong ></ td >
6 </ tr >
7 < xsl:for-each select ="/Book/RecSubjCategories/Category" >
8 < tr >
9 < td >< xsl:value-of select ="text()" /></ td >
10 </ tr >
11 </ xsl:for-each >
12 </ table >
13 </ div > <!-- 这行出了问题 -->
14 </ XML >
出问题的原因是,由于上面的<DIV xmlns:xsl="http://www.w3.org/TR/wd-xsl">是大写的,而结尾确是小写,自然会有问题,所以把</div>改为</DIV>就可以了,即:
2 <DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
3 < table border ="0" cellpadding ="1" >
4 < tr >
5 < td >< strong > Categories </ strong ></ td >
6 </ tr >
7 < xsl:for-each select ="/Book/RecSubjCategories/Category" >
8 < tr >
9 < td >< xsl:value-of select ="text()" /></ td >
10 </ tr >
11 </ xsl:for-each >
12 </ table >
13 </ div > <!-- 这行出了问题 -->
14 </ XML >
<
XML
id
=categoryXSL>
<DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
< table border ="0" cellpadding ="1" >
< tr >
< td >< strong > Categories </ strong ></ td >
</ tr >
< xsl:for-each select ="/Book/RecSubjCategories/Category" >
< tr >
< td >< xsl:value-of select ="text()" /></ td >
</ tr >
</ xsl:for-each >
</ table >
</ DIV >
</ XML >
<DIV xmlns:xsl ="http://www.w3.org/TR/wd-xsl" >
< table border ="0" cellpadding ="1" >
< tr >
< td >< strong > Categories </ strong ></ td >
</ tr >
< xsl:for-each select ="/Book/RecSubjCategories/Category" >
< tr >
< td >< xsl:value-of select ="text()" /></ td >
</ tr >
</ xsl:for-each >
</ table >
</ DIV >
</ XML >