我想用XSLT构建评论系统。下面是已经提交评论XML输入:XSLT评论系统
<in:inputs xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
<!-- Input Parameter, XPath /in:inputs/in:param[@name='story_id'] -->
<in:param name="story_id">182485599</in:param>
<!-- Function Call Result (0 ms), XPath /in:inputs/in:result[@name='LoggedInWebUserID'] -->
<in:result name="LoggedInWebUserID">233459</in:result>
<!-- Function Call Result (9 ms), XPath /in:inputs/in:result[@name='XML_Comment']/root -->
<in:result name="XML_Comment">
<root xmlns="">
<Comments CommentID="1" ResponseCommentID="0" WebUserID="123456" FULL_NAME="Osikhuemhe Abulume" Comment="test comment!!!!" DateSubmitted="Feb 20 2013 1:34PM"/>
<Comments CommentID="2" ResponseCommentID="0" WebUserID="261337" FULL_NAME="Phillip Lowe" Comment="test comment2!!!!" DateSubmitted="Feb 20 2013 5:14PM"/>
<Comments CommentID="3" ResponseCommentID="1" WebUserID="000007" FULL_NAME="Norman Abbott" Comment="my response" DateSubmitted="Feb 20 2013 5:14PM"/>
<Comments CommentID="4" ResponseCommentID="0" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="Not impressed..." DateSubmitted="Feb 20 2013 4:10PM"/>
<Comments CommentID="5" ResponseCommentID="0" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="blah blah blah. " DateSubmitted="Feb 20 2013 4:11PM"/>
<Comments CommentID="6" ResponseCommentID="0" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="dfsfs" DateSubmitted="Feb 20 2013 4:14PM"/>
<Comments CommentID="7" ResponseCommentID="5" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="this is a response to blah blah blah." DateSubmitted="Feb 20 2013 4:52PM"/>
<Comments CommentID="8" ResponseCommentID="3" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="I don't agree with Norman. Terrible response." DateSubmitted="Feb 20 2013 5:39PM"/>
<Comments CommentID="9" ResponseCommentID="4" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="I'm impressed." DateSubmitted="Feb 20 2013 5:43PM"/>
<Comments CommentID="10" ResponseCommentID="1" WebUserID="233459" FULL_NAME="Tamara Failor" Comment="I've got something to say!" DateSubmitted="Feb 20 2013 6:34PM"/>
</root>
</in:result>
这应该像新闻报道的任何其他评论系统(参见:http://www.npr.org/2013/02/20/172384724/when-a-bad-economy-means-working-forever)
那就是 - 新的回应(ResponseCommentID = 0)总会被推到左边。
对这些评论的回应将在下面缩进。 (我现在不在意凹痕,我想得到回复评论,以便互相下注)
有两个部分我被卡住了。第一部分是每篇文章应该怎样叫:
<xsl:variable name="story_id" select="/in:inputs/in:param[@name='story_id']" />
<xsl:variable name="root" select="/in:inputs/in:result[@name='XML_Comment']/root" />
<xsl:for-each select="$root/Comments[@ResponseCommentID=0]">
<!-- call the template to plot the first comment down -->
<xsl:call-template name="thecomment">
<xsl:with-param name="CommentID"><xsl:value-of select="@CommentID" /></xsl:with-param>
<xsl:with-param name="ResponseCommentID"><xsl:value-of select="@ResponseCommentID" /></xsl:with-param>
</xsl:call-template>
<!-- if the comment has any responses, put those underneath the root -->
<xsl:for-each select="$root/Comments[current()/@CommentID = $root/Comments/@ResponseCommentID]">
<xsl:call-template name="thecomment">
<xsl:with-param name="CommentID"><xsl:value-of select="@CommentID" /></xsl:with-param>
<xsl:with-param name="ResponseCommentID"><xsl:value-of select="@ResponseCommentID" /></xsl:with-param>
</xsl:call-template>
</xsl:for-each>
的(也是非常错误的)结束递归模板的一部分:
<xsl:if test="@CommentID = $root/Comments/@ResponseCommentID">
<xsl:call-template name="thecomment">
<xsl:with-param name="CommentID"><xsl:attribute name="value"><xsl:value-of select="@CommentID[@CommentID = $root/Comments/@ResponseCommentID]" /></xsl:with-param>
<xsl:with-param name="ResponseCommentID"><xsl:attribute name="value"><xsl:value-of select="@ResponseCommentID[@CommentID = $root/Comments/@ResponseCommentID]" /></xsl:with-param>
</xsl:call-template>
</xsl:if>
如果有人可以把我在正确的我非常感谢它。如果需要更多信息,请告诉我。实际的模板“评论”只是简单地将评论传递给它并按照我想要的方式进行格式化。
真棒蒂姆。感谢您的回应。我对密钥不太熟悉,但您的解决方案完全符合我期望的要求!我还应该提到使用缩进列表非常聪明。我不知道为什么我没有想到这一点。 – user673869 2013-02-25 18:51:48