XSL完整示例子
xml:a.xml
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="a.xsl"?>
<ZOO>
<ANIMAL A=’A’>
<NAME>PIG</NAME>
<AGE>20</AGE>
</ANIMAL>
<ANIMAL A=’B’>
<NAME>DOG</NAME>
<AGE>30</AGE>
</ANIMAL>
<ANIMAL A=’C’>
<NAME>FISH</NAME>
<AGE>40</AGE>
</ANIMAL>
<ANIMAL A=’D’>
<NAME>BIRD</NAME>
<AGE>10</AGE>
</ANIMAL>
</ZOO>
xsl:a.xsl
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="ZOO/ANIMAL"/>
</xsl:template>
<xsl:template match="ANIMAL" >
<html>
<body>
<table border="2">
<tr>
<th>属性A</th><th>姓名</th><th>年龄</th>
</tr>
<xsl:for-each select="." >
<xsl:sort select="AGE"/><!-- 这句为什么没用 -->
<tr>
<td>
<!-- 使用模板 @A代表属性-->
<xsl:apply-templates select="@A"/>
</td>
<xsl:apply-templates select="NAME"/>
<xsl:apply-templates select="AGE"/>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<!-- 定义模板 模板中可以写脚本语句 -->
<xsl:template match="NAME">
<td>
<!-- if -->
<xsl:if test=".=’PIG’"><!-- "." 代表当前节点 -->
这是猪
</xsl:if>
<xsl:if test=".=’DOG’">
这是狗
</xsl:if>
<xsl:if test=" .=’FISH’">
<xsl:value-of select="."/>
</xsl:if>
<xsl:if test=" .=’BIRD’">
<xsl:value-of select="."/>
</xsl:if>
</td>
</xsl:template>
<xsl:template match="AGE">
<td>
<xsl:if test=".=10">
<font color="red"><xsl:value-of select="."/></font>
</xsl:if>
<xsl:if test=".!=10">
<xsl:value-of select="."/>
</xsl:if>
</td>
</xsl:template>
<xsl:template match="@A">
<!-- choose -->
<xsl:choose>
<xsl:when test=".=’A’">
<font color=’red’><xsl:value-of select="."/></font>
</xsl:when>
<xsl:when test=".=’B’">
<font color=’black’><xsl:value-of select="."/></font>
</xsl:when>
<xsl:when test=".=’C’">
<font color=’yellow’><xsl:value-of select="."/></font>
</xsl:when>
<xsl:otherwise>
<font color=’blue’><xsl:value-of select="."/></font>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
推荐到鲜果:


评论