读书人

xsl中的when判断并显示的有关问题

发布时间: 2012-03-16 16:34:56 作者: rapoo

xsl中的when判断并显示的问题
resume.xml文件如下:
<?xml version= "1.0 " encoding= "GB2312 "?>
<?xml-stylesheet href= "resume.xsl " type= "text/xsl "?>
<document>
<resume>
<name> 阿明 </name>
<sex> 男 </sex>
<birthday> 1977 </birthday>
<skill> 数据库设计与维护、WEB开发 </skill>
</resume>
<resume>
<name> txf </name>
<sex> 男 </sex>
<birthday> 1979 </birthday>
<skill> asp.net,sqlserver,oracle,xml与xsl </skill>
</resume>
</document>

resume.xsl文件如下:
<?xml version= "1.0 " encoding= "GB2312 "?>
<xsl:stylesheet xmlns:xsl= "http://www.w3.org/TR/WD-xsl ">
<xsl:template match= "/ ">
<html>
<head>
<title> 自己开发的第一个xsl </title>
<link href= "../style.css " rel= "stylesheet " type= "text/css " />
</head>
<body>
<table border= "1 ">
<tr>
<th> 姓名 </th>
<th> 性别 </th>
<th> 生日 </th>
<th> 技能 </th>
</tr>
<xsl:apply-templates select= "document/resume "/>
</table>
</body>
</html>
</xsl:template>

<xsl:template match= "resume[index()$lt$2] ">
<tr>
<xsl:apply-templates select= "name "/>
<xsl:apply-templates select= "sex "/>
<xsl:apply-templates select= "birthday "/>
<xsl:apply-templates select= "skill "/>
</tr>
</xsl:template>


<xsl:template match= "name ">
<td>

<xsl:value-of/>
</td>
</xsl:template>

<xsl:template match= "sex ">
<td>

<xsl:value-of/> </td>
</xsl:template>

<xsl:template match= "birthday ">

<td>
<xsl:if text= ".[value()= '1979] ">
<xsl:attribute name= "style "> color:red </xsl:attribute>
<xsl:value-of/>
</xsl:if>

</td>
</xsl:template>
<xsl:template match= "skill ">


<td> <xsl:value-of/> </td>
</xsl:template>
</xsl:stylesheet>

我想将生日等于1979的返红色,可是结果是都反红色了,请问错在哪里?

[解决办法]
你把if 的test写成text了。改成下面即可
<td>
<xsl:if test= ".[text()= '1979 '] ">
<xsl:attribute name= "style "> color:red </xsl:attribute>
</xsl:if>
<xsl:value-of/>
</td>

读书人网 >XML SOAP

热点推荐