自反+递归 实现评论的无限引用 XSLT版
看了张子阳同学的自反+递归 实现评论的无限引用,自已来个xslt版。各位见笑了!![]()
为人方便起见,评论数据直接保存在XML中.
1
<?xml version="1.0" encoding="utf-8"?>2
<?xml-stylesheet type="text/xsl" href="Comments.xslt"?>3
<Comments>4
<List ArticleID="1">5
<Item ID="1" RefID="0" PostDate="2009-08-01" IP="123.1.23.2">6
<Comment>哥已不在江湖,江湖上却有哥的传说!</Comment>7
<User>火星网友</User>8
</Item>9
<Item ID="2" RefID="1" PostDate="2009-08-01" IP="123.1.23.1">10
<Comment>哥抽的不是烟,是寂寞!</Comment>11
<User>北京网友</User>12
</Item>13
<Item ID="3" RefID="2" PostDate="2009-08-01" IP="123.1.23.3">14
<Comment>哥玩的不是游戏,是寂寞!</Comment>15
<User>广州网友</User>16
</Item>17
<Item ID="4" RefID="3" PostDate="2009-08-01" IP="123.1.23.3">18
<Comment>哥敲的不是代码,是寂寞!</Comment>19
<User>西安网友</User>20
</Item>21
<Item ID="5" RefID="0" PostDate="2009-08-01" IP="123.1.23.3">22
<Comment>曾哥纯爷们,铁血史泰龙!</Comment>23
<User>上海网友</User>24
</Item>25
</List>26
</Comments>
/* CSS Document */
body{
font-size:12px;
}
#CommentList{
width:540px;
margin:20px auto;
border-bottom:1px solid #ccc;
background:#EDF0F1;
}
.Title{
padding:5px 0;padding:0;color:#1f3a87;font-size:12px;
}
.Title span{
float:right;color:#666;font-size:12px;
}
.Comment{
padding:5px 8px;background:#f8fcff;border:1px solid #aaa;font-size:14px;border-bottom:none;
}
.Comment div{
background:#ffe;padding:3px;border:1px solid #aaa;line-height:140%;margin-bottom:5px;
}
.Comment div span{
color:#1f3a87;font-size:12px;
}
1
<?xml version="1.0" encoding="utf-8"?>2
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">3
<xsl:output method="xhtml" version="1.0" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"/>4

5
<xsl:template match="/">6

7
<html xmlns="http://www.w3.org/1999/xhtml">8
<head>9
<title>10
Comments11
</title>12
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />13
<meta name="copyright" content="Sfzz" />14
<meta content="NSSystem" name="GENERATOR" />15
<meta content="Sfzz" name="keywords" />16
<meta content="Sfzz." name="description" />17
<link type="text/css" rel="stylesheet" href="style.css" />18
</head>19

20
<body>21

22
<div id="CommentList">23
<xsl:for-each select="/Comments/List/Item">24
<div class="Comment">25
<p class="Title"><span><xsl:value-of select="@PostDate" /> 发表</span><xsl:value-of select="User" /></p>26
<xsl:call-template name="GetRefList">27
<xsl:with-param name="RefID" select="@RefID"/>28
<xsl:with-param name="CommentList" select="/Comments/List/Item" />29
</xsl:call-template>30
<xsl:value-of select="Comment" />31
</div>32
</xsl:for-each>33
</div>34
</body>35
</html>36
</xsl:template>37
38
<xsl:template name="GetRefList">39
<xsl:param name="RefID"></xsl:param>40
<xsl:param name="CommentList"></xsl:param>41

42
<xsl:if test="$RefID > 0">43
<div>44
<xsl:call-template name="GetRefList">45
<xsl:with-param name="RefID" select="$CommentList[@ID=$RefID]/@RefID"/>46
<xsl:with-param name="CommentList" select="/Comments/List/Item" />47
</xsl:call-template>48
<span><xsl:value-of select="$CommentList[@ID=$RefID]/User" /> 原帖</span><br/>49
<xsl:value-of select="$CommentList[@ID=$RefID]/Comment" />50
</div>51
</xsl:if>52
</xsl:template>53
54
</xsl:stylesheet>
说明:html及css部分来自张子阳的内容

浙公网安备 33010602011771号