xsl的语法解析

公司的检索技术用到了xsl和xml合成html技术。闲暇之余,学习了xsl语法,写了个小例子,以总结归纳。


  • xml示例如下(hello.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<catalog>
<cd index="title0">
<title >Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
<a>a</a>
</cd>
<cd index="title1">
<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 index="title2">
<title name="newTitle">kaka your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1961</year>
</cd>
<cd  index="title3">
<title>亚里亚</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1971</year>
</cd>
<cd  index="title4">
<title>tt</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1971</year>
</cd>

</catalog>
  • xsl示例如下(hello.xsl):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes" method="html" encoding="UTF-8" />
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>

<xsl:variable name="header">
	<tr bgcolor="#9acd32">
		<th>Title</th>
		<th>Artist</th>
		<th>COM.</th>
		<th>year</th>
		<th>a的值</th>
  </tr>
</xsl:variable>
<xsl:variable name="header2" select="$header" />	
<table border="1">
<xsl:copy-of select="$header" />
<xsl:copy-of select="$header2" />
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
<th>COM.</th>
<th>year</th>
<th>a的值</th>
</tr>
<xsl:for-each select="catalog/cd[year &lt; 1986]">
<!--> 
合法的过滤运算符:
:=  (等于) 
:!= (不等于)
:&lt; (小于)
:&gt; (大于)  

如需对结果进行排序,只要简单地在 XSL 文件中的 <xsl:for-each> 元素内部添加一个 <xsl:sort> 
 <-->
 <xsl:sort select="year"/>  
<tr>
 <xsl:if test="title='tt'">
	<td>
   <xsl:value-of select="title"/>这是tt
 </td>
	
</xsl:if>
<xsl:if test="title!='tt'">
	<td>
  <xsl:value-of select="title"/>  编号: <xsl:value-of select="@index"/>           标题序号:<xsl:value-of select="title/@name"/> <!--> 取属性值 <-->
  </td>
	
</xsl:if>

<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="company"/>
</td>

<xsl:choose> 
<xsl:when test="year &gt; 1980"> 
<td>
<xsl:value-of select="year"/><h6>80年代以后</h6>
</td>
</xsl:when>
<xsl:when test="year &lt; 1980"> 
<td>
<xsl:value-of select="year"/><h6>80年代以前</h6>
</td>
</xsl:when>
<xsl:otherwise> 
<td>
<xsl:value-of select="year"/><h6>未识别</h6>
</td>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="a">  
<td>
<xsl:value-of select="a"/>
</td>
</xsl:if>

<xsl:if test="not(a) ">  
<td>
不存在a
</td>
</xsl:if>
<!--> 
判断多个节点是否同时存在:    <xsl:if test="a | b | c">  
判断多个节点是否至少有一个存在:    <xsl:if test="a or b or c">  
<-->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
  • 展示:将两者放在同一个目录下,即可浏览器访问hello.xml查看效果:

  • 其他常用语法总结:(未完)

猜你喜欢

转载自blog.csdn.net/weixin_36087172/article/details/82684264
xsl