lexy

          支持開(kāi)源,尊重他人的勞動(dòng)!

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            17 隨筆 :: 0 文章 :: 9 評(píng)論 :: 0 Trackbacks
          XSLT初學(xué)最簡(jiǎn)單例子

          最近了解了下XSLT,寫(xiě)了個(gè)簡(jiǎn)單的例子,很簡(jiǎn)單,不過(guò)對(duì)從來(lái)沒(méi)接觸過(guò)XSL的新手來(lái)說(shuō),足夠了。

          一共三個(gè)文件:xsl,xml,html,放在同一目錄下就可以了。
          用瀏覽器打開(kāi)xml文件和html文件,效果是一樣的。(IE 6.0+)

          ------------------ test.xsl -----------------------

          <?xml version="1.0" encoding="iso-8859-1"?>
          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">

          <html>

          <head>

          <title>First XSLT example</title>

          </head>

          <body>

          <table border="1" width="300" align="center">
          ?? ?<tr>
          ?? ??? ?<th>ID</th>
          ?? ??? ?<th>Name</th>
          ?? ??? ?<th>Age</th>
          ?? ?</tr>
          ?? ?<xsl:for-each select="test/people">?? ?
          ?? ??? ?<xsl:sort select="id"/>
          ?? ??? ??? ?<xsl:if test="age!=0">
          ?? ??? ??? ??? ?<xsl:choose >
          ?? ??? ??? ??? ??? ?<xsl:when test="age>20">
          ?? ??? ??? ??? ??? ??? ?<tr>
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="id"/></td>
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="name"/></td>
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="age"/></td>
          ?? ??? ??? ??? ??? ??? ?</tr>
          ?? ??? ??? ??? ??? ?</xsl:when>
          ?? ?
          ?? ??? ??? ??? ??? ?<xsl:otherwise>
          ?? ??? ??? ??? ??? ??? ?<tr bgcolor="red">
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="id"/></td>
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="name"/></td>
          ?? ??? ??? ??? ??? ??? ??? ?<td><xsl:value-of select="age"/></td>
          ?? ??? ??? ??? ??? ??? ?</tr>
          ?? ??? ??? ??? ??? ?</xsl:otherwise>
          ?? ??? ??? ??? ?</xsl:choose>
          ?? ??? ??? ?</xsl:if>?? ?
          ?? ?</xsl:for-each>
          </table>

          <br/>

          <table border="3" width="300" align="center">
          ?? ?<xsl:apply-templates/>
          </table>

          </body>

          </html>

          </xsl:template>


          <xsl:template match="people">
          ?? ??? ?<tr align="center">
          ?? ??? ??? ?<td style="color:green">
          ?? ??? ??? ??? ?<xsl:apply-templates select="id"/>
          ?? ??? ??? ?</td>
          ?? ??? ??? ?<td style="color:violet">
          ?? ??? ??? ??? ?<xsl:apply-templates select="name"/>
          ?? ??? ??? ?</td>
          ?? ??? ??? ?<td style="color:blue">
          ?? ??? ??? ??? ?<xsl:apply-templates select="age"/>
          ?? ??? ??? ?</td>
          ?? ??? ?</tr>
          </xsl:template>

          <xsl:template match="id">
          ?? ?<xsl:value-of select="."/>
          </xsl:template>

          <xsl:template match="name">?? ?
          ?? ?<xsl:value-of select="."/>
          </xsl:template>

          <xsl:template match="age">?? ?
          ?? ?<xsl:value-of select="."/>
          </xsl:template>

          </xsl:stylesheet>


          ---------------------- test.xml -----------------------------------

          <?xml version="1.0" encoding="iso-8859-1"?>

          <?xml-stylesheet type="text/xsl" href="test.xsl"?>
          <test>
          ?? ?<people>
          ?? ??? ?<id>1</id>
          ?? ??? ?<name>lexy</name>
          ?? ??? ?<age>23</age>
          ?? ?</people>
          ?? ?<people>
          ?? ??? ?<id>3</id>
          ?? ??? ?<name>some 3</name>
          ?? ??? ?<age>20</age>
          ?? ?</people>
          ?? ?<people>
          ?? ??? ?<id>2</id>
          ?? ??? ?<name>some</name>
          ?? ??? ?<age>18</age>
          ?? ?</people>?? ??? ?
          ?? ?<people>
          ?? ??? ?<id>5</id>
          ?? ??? ?<name>some 5</name>
          ?? ??? ?<age>0</age>
          ?? ?</people>
          ?? ?<people>
          ?? ??? ?<id>4</id>
          ?? ??? ?<name>some 4</name>
          ?? ??? ?<age>25</age>
          ?? ?</people>
          ?? ?<people>
          ?? ??? ?<id>6</id>
          ?? ??? ?<name>some 6</name>
          ?? ??? ?<age>24</age>
          ?? ?</people>
          ?? ?
          </test>


          -------------------------- test.html ------------------------------------

          <html>
          <body>

          <script type="text/javascript">

          // Load XML
          var xml = new ActiveXObject("Microsoft.XMLDOM")
          xml.async = false
          xml.load("test.xml")

          // Load XSL
          var xsl = new ActiveXObject("Microsoft.XMLDOM")
          xsl.async = false
          xsl.load("test.xsl")

          // Transform
          document.write(xml.transformNode(xsl))

          document.write('Transform XML by javascript!');

          </script>

          </body>
          </html>

          參考:
          ?? ?http://www.w3school.com.cn/xsl/index.asp
          posted on 2007-12-29 09:13 lexy 閱讀(502) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: XSLT初學(xué)最簡(jiǎn)單例子 2009-04-29 21:01 apple pig
          非常感謝  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 阿图什市| 铁岭县| 军事| 抚州市| 武城县| 汾西县| 慈利县| 瓮安县| 保定市| 金门县| 青神县| 从江县| 玛曲县| 平昌县| 平舆县| 衡阳县| 全州县| 舟山市| 尼勒克县| 大姚县| 南漳县| 锦州市| 伊通| 南岸区| 额尔古纳市| 崇礼县| 大冶市| 台湾省| 金寨县| 西藏| 桐庐县| 台中市| 原平市| 淮阳县| 麻江县| 邻水| 榆林市| 凯里市| 大邑县| 沙田区| 泾源县|