XSL语法学习

正规教程:http://www.w3school.com.cn/xsl/index.asp

标签手册:http://www.w3school.com.cn/xsl/xsl_w3celementref.asp

函数手册:http://www.w3school.com.cn/xsl/xsl_functions.asp


XSL头部定义:

因为XSL是标准的XML格式,所有第一行是xml的标准说明,第二行XSL的说明:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
如需访问 XSLT 的元素、属性以及特性,我们必须在文档顶端声明 XSLT 命名空间。即

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


XSL内容部分:

内容部分严格按照xml的规则来的,即都是由开始和结束节点构成的元素队。但必须要包含在xsl:template节点下

<xsl:template match="/">


XSL的常用语法:

<xsl:template match=“xpath”>  </xsl:template>   模板节点,所有要呈现的内容均要放在这类节点里

<xsl:value-of select="xpath" />      取值节点,获取对应xpath节点的文本值

<xsl:for-each select="xpath">  </xsl:for-each>    循环节点,循环指定xpath对应的节点

<xsl:if test="expression">  </xsl:if>     判断节点,如果条件正确则显示节点之间的内容

<xsl:choose>                  多重条件选择节点,类型case语法
  <xsl:when test="expression">
    ... 输出 ...
  </xsl:when>
  <xsl:when test="expression">
    ... 输出 ...
  </xsl:when>
  <xsl:otherwise>
    ... 输出 ....
  </xsl:otherwise>
</xsl:choose>

<xsl:sort select="nodename"/>     排序节点,按照指定的节点名升序排序

<xsl:apply-templates select="xpath"/>     应用模板,指定当前所在template节点范围下哪些子节点需要应用其对应的模板。没有select属性则表示所有子节点都应用模板;若指定则对指定的节点应用对应的模板,如果其没有对应的模板则直接显示该节点的文本内容

在XML中引用XSL文件:

<?xml-stylesheet type="text/xsl" href="你的xsl文件路径"?>


XML样例:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="你的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>
	<cd>
		<title>Hide your heart</title>
		<artist>Bonnie Tyler</artist>
		<country>UK</country>
		<company>CBS Records</company>
		<price>9.90</price>
		<year>1988</year>
	</cd>
	<cd>
		<title>Greatest Hits</title>
		<artist>Dolly Parton</artist>
		<country>USA</country>
		<company>RCA</company>
		<price>9.90</price>
		<year>1982</year>
	</cd>
	<cd>
		<title>Still got the blues</title>
		<artist>Gary Moore</artist>
		<country>UK</country>
		<company>Virgin records</company>
		<price>10.20</price>
		<year>1990</year>
	</cd>
</catalog>


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>
  <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:sort select="price"/>
	  <xsl:if test="price > 5">
      <tr>
        <td><xsl:apply-templates 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>
        <td><xsl:value-of select="price"/></td>
      </tr>
	  </xsl:if>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

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

</xsl:stylesheet>


PS:

需要在html标签的属性里引用xml节点内容的,使用格式为{nede name}, 如下:

<img src='{screenshot}' />

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值