XML学习错误(1):大小写的问题

本文介绍了一位编程新手在学习XMLDOM过程中遇到的一个具体错误案例,详细解释了如何定位问题并修复了因XSL标签大小写不一致导致的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       笔者是一个编程菜鸟,最近努力学习Ajax技术,感觉JS不太好驯服,但是本人依然不断努力。笔者认为把自己的编程错误记录下来,是对经验的积累,经验是慢慢积累的嘛。
       今天就是在学习XML DOM编程过程中,出现了一个错误,浏览器总是告诉我有一个参数无效,后来经过认真研究终于找到了。
       先把代码给大家看看吧。
 
  1None.gif<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2None.gif<html>
  3None.gif<head>
  4None.gif<title></title>
  5None.gif<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  6None.gif<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  7None.gif</head>
  8None.gif<body onload="initializeBook();">
  9None.gif  <h1>  Wrox Press </h1>
 10None.gif  <h3> Book Information </h3>
 11None.gif  <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
 12None.gif    <TR>
 13None.gif        <TD>Title:</TD><TD><input id="txtTitle"></TD>
 14None.gif    </TR>
 15None.gif    <TR>
 16None.gif        <TD>Publisher:</TD><TD><input id="txtPublisher"></TD>
 17None.gif    </TR>
 18None.gif    <TR>
 19None.gif        <TD>Published Date:</TD><TD><input id="txtPubDate"></TD>
 20None.gif    </TR>
 21None.gif    <TR>
 22None.gif        <TD>Abstract:</TD><TD><input id="txtAbstract"></TD>
 23None.gif    </TR>
 24None.gif    <TR>
 25None.gif        <TD>Pages</TD><TD><input id="txtPages"></TD>
 26None.gif    </TR>
 27None.gif    <TR>
 28None.gif        <TD>ISBN:</TD><TD><input id="txtISBN"></TD>
 29None.gif    </TR>
 30None.gif    <TR>
 31None.gif        <TD>Price:</TD><TD><input id="txtPrice"></TD>
 32None.gif    </TR>
 33None.gif    
 34None.gif</TABLE>
 35None.gif
 36None.gif   <input type="button" id="btnUpdate" value="Update Book Info" onclick="updateBookInfo();" />
 37None.gif   
 38None.gif   <h3>Author:</h3>
 39None.gif   <table>
 40None.gif     <tr>
 41None.gif        <td>Author:</td><td><input id="txtAuthor"></td>
 42None.gif     </tr>
 43None.gif   </table>
 44None.gif   <input type="button" id="btnAddAuthor" value="Add Author" onclick="addAuthor();" />
 45None.gif   <h3>Catagories:</h3>
 46None.gif    <table>
 47None.gif     <tr>
 48None.gif        <td>Category:</td><td><input id="txtCategory"></td>
 49None.gif     </tr>
 50None.gif   </table>
 51None.gif   <input type="button" id="btnAddCategory" value="Add category" onclick="addCategory();"/>
 52None.gif   
 53None.gif    <XML id=docBook>
 54None.gif        <Book>
 55None.gif        </Book>
 56None.gif    </XML>
 57None.gif    
 58ExpandedBlockStart.gifContractedBlock.gif  <script>dot.gif
 59InBlock.gif  var docBook;
 60InBlock.gif  function initializeBook()
 61ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 62InBlock.gif    docBook=document.all("docBook").XMLDocument;
 63InBlock.gif    docBook.async=false;
 64InBlock.gif    renderElements();
 65ExpandedSubBlockEnd.gif  }

 66InBlock.gif  
 67InBlock.gif  function createOrReplaceElement(sElementName,sElementValue,elementParent)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 69InBlock.gif    var elementItem;
 70InBlock.gif    var textValue;
 71InBlock.gif    var nodelistOldItem;
 72InBlock.gif    
 73InBlock.gif    elementItem=docBook.createElement(sElementName);
 74InBlock.gif    textValue=docBook.createTextNode(sElementValue);
 75InBlock.gif    elementItem.appendChild(textValue);
 76InBlock.gif    
 77InBlock.gif    nodelistOldItem=elementParent.getElementsByTagName(sElementName);
 78InBlock.gif    if (nodelistOldItem.length>0)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 80InBlock.gif       elementParent.replaceChild(elementItem,nodelistOldItem.item(0));
 81ExpandedSubBlockEnd.gif    }

 82InBlock.gif    else
 83ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 84InBlock.gif       elementParent.appendChild(elementItem);
 85ExpandedSubBlockEnd.gif    }

 86InBlock.gif    
 87ExpandedSubBlockEnd.gif  }

 88InBlock.gif  
 89InBlock.gif  function updateBookInfo()
 90ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 91InBlock.gif    createOrReplaceElement("Title",txtTitle.value,docBook.documentElement);
 92InBlock.gif    createOrReplaceElement("Publisher",txtPublisher.value,docBook.documentElement);
 93InBlock.gif    createOrReplaceElement("PubDate",txtPubDate.value,docBook.documentElement);
 94InBlock.gif    createOrReplaceElement("Abstract",txtAbstract.value,docBook.documentElement);
 95InBlock.gif    createOrReplaceElement("Pages",txtPages.value,docBook.documentElement);
 96InBlock.gif    createOrReplaceElement("ISBN",txtISBN.value,docBook.documentElement);
 97InBlock.gif    createOrReplaceElement("Price",txtPubDate.value,docBook.documentElement);
 98InBlock.gif    
 99InBlock.gif    renderElements();      
100ExpandedSubBlockEnd.gif  }

101InBlock.gif  
102InBlock.gif  function addAuthor()
103ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
104InBlock.gif    var elementAuthor;
105InBlock.gif    var textAuthor;
106InBlock.gif    var nodelistAuthors;
107InBlock.gif    var elementAuthors;
108InBlock.gif    
109InBlock.gif    elementAuthor=docBook.createElement("Author");
110InBlock.gif    textAuthor=docBook.createTextNode(txtAuthor.value);
111InBlock.gif    elementAuthor.appendChild(textAuthor);
112InBlock.gif    nodelistAuthors=docBook.getElementsByTagName("Authors");
113InBlock.gif    if(nodelistAuthors.length==0)
114ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
115InBlock.gif      elementAuthors=docBook.createElement("Authors");
116InBlock.gif      docBook.documentElement.appendChild(elementAuthors);
117ExpandedSubBlockEnd.gif    }

118InBlock.gif    else
119ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
120InBlock.gif      elementAuthors=nodelistAuthors.item(0);
121ExpandedSubBlockEnd.gif    }

122InBlock.gif    elementAuthors.appendChild(elementAuthor);
123InBlock.gif    renderElements();
124ExpandedSubBlockEnd.gif  }

125InBlock.gif  
126InBlock.gif  function addCategory()
127ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
128InBlock.gif    var elementCategory;
129InBlock.gif    var textCategory;
130InBlock.gif    var nodelistRecSubjCategories;
131InBlock.gif    var elementRecSubjCategories;
132InBlock.gif    
133InBlock.gif    elementCategory=docBook.createElement("Category");
134InBlock.gif    textCategory=docBook.createTextNode(txtCategory.value);
135InBlock.gif    elementCategory.appendChild(textCategory);
136InBlock.gif    nodelistRecSubjCategories=docBook.getElementsByTagName("RecSubjCategories");
137InBlock.gif    
138InBlock.gif    if(nodelistRecSubjCategories.length==0)
139ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
140InBlock.gif       elementRecSubjCategories=docBook.createElement("RecSubjCategories");
141InBlock.gif       docBook.documentElement.appendChild(elementRecSubjCategories);
142ExpandedSubBlockEnd.gif    }
  
143InBlock.gif    else
144ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
145InBlock.gif       elementRecSubjCategories=nodelistRecSubjCategories.item(0);
146ExpandedSubBlockEnd.gif    }

147InBlock.gif    
148InBlock.gif    elementRecSubjCategories.appendChild(elementCategory);
149InBlock.gif    
150InBlock.gif    renderElements();
151ExpandedSubBlockEnd.gif  }

152InBlock.gif  
153InBlock.gif  function renderElements()
154ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
155InBlock.gif    document.all("divRawXML").innerText=docBook.xml;
156InBlock.gif    bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
157InBlock.gif    authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
158InBlock.gif    categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);
159InBlock.gif    
160ExpandedSubBlockEnd.gif  }

161InBlock.gif  
162ExpandedBlockEnd.gif  
163None.gif
</script>
164None.gif
165None.gif
166None.gif    <XML id=bookXSL>
167None.gif     <DIV xmlns:xsl="http://www.w3.org/TR/wd-xsl">
168None.gif       <xsl:choose>
169None.gif         <xsl:when test="/Book/Title[. $ne$ '']">
170None.gif           <table border="0" cellpadding="1">
171None.gif             <tr>
172None.gif               <td>Title:</td><td><xsl:value-of select="/Book/Title"/></td>
173None.gif             </tr>
174None.gif             <tr>
175None.gif               <td>Publisher:</td><td><xsl:value-of select="/Book/Publisher"/></td>
176None.gif             </tr>
177None.gif             <tr>
178None.gif               <td>Published Date:</td><td><xsl:value-of select="/Book/PubDate"/></td>
179None.gif             </tr>
180None.gif             <tr>
181None.gif               <td>Abstract:</td><td><xsl:value-of select="/Book/Abstract"/></td>
182None.gif             </tr>
183None.gif             <tr>
184None.gif               <td>Pages:</td><td><xsl:value-of select="/Book/Pages"/></td>
185None.gif             </tr>
186None.gif             <tr>
187None.gif               <td>ISBN:</td><td><xsl:value-of select="/Book/ISBN"/></td>
188None.gif             </tr>
189None.gif              <tr>
190None.gif               <td>Price:</td><td><xsl:value-of select="/Book/Price"/></td>
191None.gif             </tr>
192None.gif           </table>
193None.gif         </xsl:when>
194None.gif         <xsl:otherwise>
195None.gif           Book Information not yet specified.
196None.gif         </xsl:otherwise>
197None.gif       </xsl:choose>
198None.gif     </DIV>
199None.gif      
200None.gif    </XML>
201None.gif    
202None.gif    <XML id=authorXSL>
203None.gif       <DIV xmlns:xsl="http://www.w3.org/TR/wd-xsl">
204None.gif        <table border="0" cellpadding="1">
205None.gif          <tr>
206None.gif            <td><strong>Authors</strong></td>
207None.gif          </tr>
208None.gif          <xsl:for-each select="/Book/Authors/Author">
209None.gif           <tr>
210None.gif             <td><xsl:value-of select="text()" /></td>
211None.gif           </tr>
212None.gif          </xsl:for-each>
213None.gif         </table>
214None.gif       </DIV>
215None.gif    </XML>
216None.gif    
217None.gif    <XML id=categoryXSL>
218None.gif      <DIV xmlns:xsl="http://www.w3.org/TR/wd-xsl">
219None.gif        <table border="0" cellpadding="1">
220None.gif          <tr>
221None.gif            <td><strong>Categories</strong></td>
222None.gif          </tr>
223None.gif          <xsl:for-each select="/Book/RecSubjCategories/Category">
224None.gif             <tr>
225None.gif               <td><xsl:value-of select="text()" /></td>
226None.gif             </tr>
227None.gif          </xsl:for-each>
228None.gif        </table>
229None.gif      </DIV>
230None.gif    </XML>
231None.gif    
232None.gif    <hr />
233None.gif    <h2>Book Information</h2>
234None.gif    <div id="bookInfo"></div>
235None.gif    <div id="authorTable"></div>
236None.gif    <div id="categoryTable"></div>
237None.gif    <hr/>
238None.gif    The Text expression of the current contents of the DOM tree is:
239None.gif   <DIV id="divRawXML"></DIV>
240None.gif  
241None.gif
242None.gif</body>
243None.gif</html>
244None.gif
245None.gif

出现问题的代码是这一行:
   
1None.giffunction renderElements()
2ExpandedBlockStart.gifContractedBlock.gif  dot.gif{
3InBlock.gif    document.all("divRawXML").innerText=docBook.xml;
4InBlock.gif    bookInfo.innerHTML=docBook.transformNode(bookXSL.documentElement);
5InBlock.gif    authorTable.innerHTML=docBook.transformNode(authorXSL.documentElement);
6InBlock.gif    categoryTable.innerHTML=docBook.transformNode(categoryXSL.documentElement);//出现问题的代码
7InBlock.gif    
8ExpandedBlockEnd.gif  }
      可见,是categoryXSL.documentElement这个参数出了问题,也就是XSL出了问题,但是笔者查了半天也没搞明白,最后终于找到了代码的问题,原来出现错误的代码是这样的:
     
 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>就可以了,即:
 <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>

转载于:https://www.cnblogs.com/tyrael007/archive/2006/09/21/511093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值