擷取XML資料並使用樣式表格式化

來源:互聯網
上載者:User

        使用樣式表格式化獲得的XML資料,這個如果使用單獨一個XML檔案是完全可以的,在XML檔案裡面嵌入SXL檔案,但是現在不要這麼作,直接擷取XML資料,然後調用XSL檔案格式化資料。
        這部分示範程式需要使用到AJAX用戶端架構,如下例子所示:
1.ceshi.jsp

<%@ page contentType="text/html;charset=GBK"%>
<script language="javascript" src="js\request.js"></script>
<script language="javascript">

function showXML(){
    var url = "Vehicles.xml";
    get_request( url, "text" );    
}

function pageChange( responseText ){ 
        var xml = new ActiveXObject( "Microsoft.XMLDOM" );
        xml.async = false;
        //load the text of xml data
        xml.loadXML( responseText );
        
        var xsl = new ActiveXObject( "Microsoft.XMLDOM" );
        xsl.async = false;
        // Load the XSL
        xsl.load( "Vehicles.xsl" );
        var div = document.getElementById( "show" );
        div.innerHTML = xml.transformNode( xsl );
        /**//*
            load()函數載入XML檔案
            loadXML()函數載入字串形式的XML資料
        */
}
</script>
<p>
  <input type="submit" name="Submit" value="提交" onclick="showXML()" />
</p>
<div id="show"></div>

 

2.Vehicles.xml

<?xml version="1.0" encoding="gb2312"?>
<!--?xml-stylesheet href='Vehicles.xsl' type='text/xsl'?-->
<vehicles>
   <vehicle year="1002" make="Land Rover" model="Discovery">
         <mileage>36500</mileage>
         <color>black</color>
         <price>$32999</price>
   </vehicle>
</vehicles>

3.Vehicles.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD=xsl">
  <xsl:template match="/">
     <html>
       <head>
          <title>uuu</title>
       </head>
       <body>
          <table align="center" border="2">
          <tr>
            <th>Year</th>
            <th>Make</th>
            <th>Model</th>
            <th>Mileage</th>
            <th>Color</th>
            <th>Price</th>
          </tr>
          <xsl:for-each order-by="+price" select="vehicles/vehicle">
            <tr>
              <td><xsl:value-of select="@year"/></td>
              <td><xsl:value-of select="@make"/></td>
              <td><xsl:value-of select="@model"/></td>
              <td><xsl:value-of select="mileage"/></td>
              <td><xsl:value-of select="color"/></td>
              <td><xsl:value-of select="price"/></td>
            </tr>
          </xsl:for-each>
          </table>
       </body>
     </html>
  </xsl:template>
</xsl:stylesheet>

4.說明
        需要注意的XML載入後,XSL檔案的載入,以及 xml.transformNode( xsl ) 方法。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.