# XML 筆記 ###### tags: `XML` ## 介紹 >1.主要在儲存和傳輸數據 >2.實現自我描述 >3.w3c標準 >4.跟html之前差別,xml注重傳輸數據,html注重顯示數據 ## 預定義的實體引用 ``` &lt; < less than &gt; > greater than &amp; & ampersand &apos; ' apostrophe &quot; " quotation mark ``` ### CR(打字頭歸位)+ LF(紙張往下捲動一行) ## XML元素 ``` <bookstore> <book category="children"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="web"> <title>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> ``` ><title>,<author>,<year>和<price>包含文本內容,因為它們包含文本(如29.99)。 ><bookstore>和<book>具有元素內容,因為它們包含元素。 ><book>有一個屬性 (category =“children”)。 ## XML命名規則 >1.元素名稱區分大小寫 >2.元素名稱必須以字母或下劃線開頭 >3.元素名稱不能以字母xml(或XML或Xml等)開頭 >4.元素名稱可以包含字母,數字,連字符,下劃線和句點 >5.元素名稱不能包含空格 ## XML 命名空間 可使用前綴解決名稱衝突 ## XML命名空間-xmlns屬性 在XML中使用前綴時,必須定義前綴的命名空間。 命名空間可以由元素的開始標記中的xmlns屬性定義。 名稱空間聲明具有以下語法。xmlns:prefix =“URI”。 ## XMLHttpRequest 更新網頁而不重新加載頁面 在頁面加載後從服務器請求數據 在頁面加載後從服務器接收數據 將數據發送到服務器 - 在後台 ``` var xhttp = new XMLHttpRequest();//建立HttpRequest物件 xhttp.responseXML //返回響應作為XML DOM xhttp.responseText //返回響應作為一個字符串 ``` ## XML Parser(解析器) 可以將文本轉換為XML DOM對象。 ``` parser = new DOMParser(); //建立DOMParser物件 xmlDoc = parser.parseFromString(text,"text/xml");//將文檔解析成XML DOM對象 ``` ## xslt ``` template 元素用於構建模板 xsl:template match="導入xml資料" value-of 元素用於提取所選節點的值 <xsl:value-of select="xml值名稱"/> for-each 用於選擇指定節點集的每個XML元素 <xsl:for-each select="xml分層路徑"> sort 對輸出進行排序 <xsl:sort select="xml值名稱"/> if 條件判斷 <xsl:if test="expression"> ...some output if the expression is true... </xsl:if> choose 表示多個條件判斷,相較與id else <xsl:choose> <xsl:when test="expression"> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose> ```