XSL入门

1、XSL 指扩展样式表语言简介:

CSS = HTML 样式表

XSL = XML 样式表

XSL 包括三部分:

XSLT:一种用于转换 XML 文档的语言。XPath:一种用于在 XML 文档中导航的语言。XSL-FO:一种用于格式化 XML 文档的语言。

浏览器支持:详见http://www.w3school.com.cn/xsl/xsl_browsers.asp

2、第一个例子:
BOOK.XML:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
<catalog>

  <cd>
    <title>羽泉</title>
    <artist>朋友难当</artist>
    <country>中国</country>
    <company>华谊兄弟</company>
    <price>10.90</price>
    <year>2005</year>
  </cd>
</catalog>
BOOK.XSL:
<?xml version="1.0" encoding="utf-8"?>

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

<xsl:template match="/">
  <html>
	  <body>
		    <h2>My CD Collection</h2>
		    <table border="1">

			    <tr bgcolor="#9acd32">
			      <th align="left">Title</th>
			      <th align="left">Artist</th>
			      <th align="left">country</th>
			      <th align="left">company</th>
			      <th align="left">price</th>
			      <th align="left">year</th>
			    </tr>

			    <xsl:for-each select="catalog/cd">
				    <tr>
				      <td><xsl:value-of select="title"/></td>
				      <td><xsl:value-of select="artist"/></td>
				      <td><xsl:value-of select="country"/></td>
				      <td><xsl:value-of select="company"/></td>
				      <td><xsl:value-of select="price"/></td>
				      <td><xsl:value-of select="year"/></td>
				    </tr>
			    </xsl:for-each>
		    </table>
	  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
 
 
 
 
 
 
 
 

注意事项:支持中文需要将编码格式设置为UTF-8,命名空间中需要添加"xmlns="http://www.w3.org/1999/xhtml",否则乱码。

3、<xsl:stylesheet>:
	   XSL样式由一套或多套被称为模板的规则构成。
	match 属性用于关联 XML 元素和模板。match 属性也可用来为整个文档定义模板。match 属性的值是 XPath 表达式(举例,match="/" 定义整个文档)。详见上面的示例。
	match="/" 属性把此模板与 XML 源文档的根相联系。
	<xsl:template> 元素内部的内容定义了写到输出结果的 HTML 代码。
4、<xsl:value-of> 元素:
	该元素用于提取某个选定节点的值,并把值添加到转换的输出流中。
	标准样式:<xsl:value-of select="catalog/cd/title"/>
5、<xsl:for-each> 元素:
	该元素可用于选取指定的节点集中的每个 XML 元素。
	 结果过滤:<xsl:for-each select="catalog/cd[artist='Bob Dylan']">
合法的过滤运算符:
 
 
  • =  (等于)
  • != (不等于)
  • &lt; (小于)
  • &gt; (大于)
6、<xsl:sort> 元素用于对结果进行排序。
	示例:<xsl:sort select="artist"/>
	select 属性指示需要排序的 XML 元素。
7、<xsl:if> 元素用于放置针对 XML 文件内容的条件测试。
	示例:
<?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 > 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>
8、 <xsl:choose> 元素用于结合 <xsl:when> 和 <xsl:otherwise> 来表达多重条件测试。
	示例:
<?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">
      <tr>
        <td><xsl:value-of select="title"/></td>
      	<xsl:choose>
          <xsl:when test="price > 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>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
9、<xsl:apply-templates> 元素可把一个模板应用于当前的元素或者当前元素的子节点。
 示例:
<?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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值