XML基础知识

一、定义

  • 定义

XSLT(=XSL转换):将XML转换为XHTML或其他XML文档的语言。(这种XML文档即可被浏览器识别,比如HTML,XHTML)使用XPath在XML文档中进行导航。

  • 如何工作

XSLT使用XPath来定义源文档中可匹配一个或多个预定义模板的部分,一旦匹配被找到,XSLT就把源文档的匹配部分转换为结果文档。

  • 兼容性
浏览器支持版本
FF1.0.2开始支持
Mozilla支持
Netscaoe8开始支持
Opera9开始支持
IE6开始支持

二、语法

1. XSL样式表本身是一个XML文档,由XML声明起始

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

2.样式表声明

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

或者

**<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">**

3. 向XML文档添加XSL样式表引用

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

<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

<catalog>

  <cd>

    <title>Empire Burlesque</title>

    <artist>Bob Dylan</artist>

    <country>USA</country>

    <company>Columbia</company>

    <price>10.90</price>

    <year>1985</year>

  </cd>
.
.
.

</catalog>

4. <xsl:template>用于构建模板。其属性match用于关联XML元素和模板,属性值是XPath表达式,如果值为match=”/”,则定义的是整个文档。

<?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>
 <body>
   <h2>My CD Collection</h2>
   <table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
       <td>.</td>
       <td>.</td>
     </tr>
   </table>
 </body>
 </html>
</xsl:template>

</xsl:stylesheet>

5. 提取某个选定节点的值,并将值添加到转换的输出流中。仅有一个属性select,select属性的值是·一个XPath表达式,用于在XML文档中进行导航。

<table border="1">
     <tr bgcolor="#9acd32">
       <th>Title</th>
       <th>Artist</th>
     </tr>
     <tr>
      <td><xsl:value-of select="catalog/cd/title"/></td>
      <td><xsl:value-of select="catalog/cd/artist"/></td>
     </tr>
   </table>

6. 用于选取指定的节点集中每个节点。有一个属性,select值为一个XPath表达式,用于在XML文档中导航。

<table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <td><xsl:value-of select="artist"/></td>
      </tr>
      </xsl:for-each>
    </table>

6.1<xsl:sort>对结果进行排序。用在<xsl:for-each>元素内部。其属性select是XML文档中的节点名称,即XML元素;指明排序的依据。默认排序是按照英文字母顺序表。

<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>

<xsl:if>放在元素内部,对XML文件内容进行条件测试。必选的test属性指明需要求值的表达式。其中“>、<”等符号必须用实体(如&gt)表示。

<?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>
  <body>
    <h2>My CD Collection</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="catalog/cd">
      <xsl:if test="price &gt; 10">
        <tr>
          <td><xsl:value-of select="title"/></td>
          <td><xsl:value-of select="artist"/></td>
        </tr>
      </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

7. <xsl:choose>,<xsl:when>,<xsl:otherwise>针对XML文件进行多重条件测试。

 <xsl:for-each select="catalog/cd">
      <tr>
        <td><xsl:value-of select="title"/></td>
        <xsl:choose>
          <xsl:when test="price &gt; 10">
            <td bgcolor="#ff00ff">
            <xsl:value-of select="artist"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="artist"/></td>
          </xsl:otherwise>
        </xsl:choose>
      </tr>
      </xsl:for-each>

8.将模板应用于当前的元素或者当前元素的子节点。其select属性决定仅仅处理与属性匹配的子元素。select属性可以决定被处理的子元素的顺序

<?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>
<body>
<h2>My CD Collection</h2> 
<xsl:apply-templates/> 
</body>
</html>
</xsl:template>

<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/> 
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>

<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

</xsl:stylesheet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值