我正在使用XSLT和Symphony构建一个论坛,我无法在主题回复中的成员用户名旁边显示其他成员数据(例如,角色/排名,头像)。
现在让我向您展示两个XML文档然后我将解释我是如何使用它们以及我遇到问题的地方。这似乎很长,但它只是让你有一个清晰的画面。
这就是主题回复的XML。一个示例包含对主题为“测试主题”的两个回复。这里重要的一点是author/item
:
<topic-replies>
<section id="10" handle="topic-replies">Topic Replies</section>
<entry id="66">
<parent-forum>
<item id="7" handle="general" section-handle="forums" section-name="Forums">General</item>
</parent-forum>
<parent-topic>
<item id="62" handle="test-topic" section-handle="forum-topics" section-name="Forum Topics">Test Topic</item>
</parent-topic>
<body><p>Testing post...</p></body>
<date-added time="14:44" weekday="4">2012-05-03</date-added>
<author>
<item id="1" handle="admin" section-handle="members" section-name="Members">Admin</item>
</author>
</entry>
<entry id="67">
<parent-forum>
<item id="7" handle="general" section-handle="forums" section-name="Forums">General</item>
</parent-forum>
<parent-topic>
<item id="62" handle="test-topic" section-handle="forum-topics" section-name="Forum Topics">Test Topic</item>
</parent-topic>
<body><p>And here's a reply...?</p></body>
<date-added time="22:56" weekday="5">2012-05-04</date-added>
<author>
<item id="1" handle="test-user-1" section-handle="members" section-name="Members">Test User 1</item>
</author>
</entry>
</topic-replies>
这是注册会员的XML:
<user-list>
<section id="1" handle="members">Members</section>
<entry id="1">
<username handle="admin">Admin</username>
<email>admin@email.com</email>
<role id="2">
<name handle="administrator">Administrator</name>
</role>
</entry>
<entry id="2">
<username handle="test-user-1">Test User 1</username>
<email>test.user.1@email.com</email>
<role id="4">
<name handle="user">User</name>
</role>
</entry>
</user-list>
当我基于topic-replies
XML对XSLT进行编码时,我只能获取作者的用户名。如果我想要更多数据,我将不得不从user-list
获取数据。我就是这样做的,考虑到这些变量:
<xsl:variable name="user-list" select="/data/user-list/entry"/>
<xsl:variable name="reply-author" select="/data/topic-replies/entry/author/item"/>
<xsl:template match="topic-replies/entry">
<ul class="profile">
<xsl:for-each select="$user-list">
<xsl:if test="username = $reply-author">
<li><a class="{role/name/@handle}" href="{$root}/user/{username/@handle}"><xsl:value-of select="username"/></a></li>
<li><xsl:value-of select="role/name"/></li>
</xsl:if>
</xsl:for-each>
</ul>
</xsl:template>
它有效,除了在每个回复中它都会提取所有参与讨论的作者,而不是仅显示指定的作者。输出是这样的:
Test Topic
Testing post...
Admin
Administrator
Re: Test Topic
And here's a reply...?
Admin
Administrator
Test Usuer 1
User
我的问题是,如何从user-list
获取数据并将其插入topic-replies
模板?
我想我可能需要使用密钥,但这是我第一次使用密钥而我真的无法想到它背后的逻辑。现在,我真的没有线索。
提前致谢。
答案 0 :(得分:1)
发生这种情况的原因是您的user-list
和reply-author
变量包含用户列表中的所有条目以及主题回复中的所有项目。
不要重复列表中的每个用户,而是只使用该项目作者的用户条目:
<xsl:template match="topic-replies/entry">
<xsl:variable name="authorEntry" select="$user-list[username/@handle = current()/author/item/@handle]"/>
<ul class="profile">
<li>
<a class="{$authorEntry/role/name/@handle}" href="{$root}/user/{$authorEntry/username/@handle}">
<xsl:value-of select="$authorEntry/username"/>
</a>
</li>
<li>
<xsl:value-of select="$authorEntry/role/name"/>
</li>
</ul>
</xsl:template>
以下是一个完整的参考示例:
XML输入
<data>
<topic-replies>
<section id="10" handle="topic-replies">Topic Replies</section>
<entry id="66">
<parent-forum>
<item id="7" handle="general" section-handle="forums" section-name="Forums">General</item>
</parent-forum>
<parent-topic>
<item id="62" handle="test-topic" section-handle="forum-topics" section-name="Forum Topics">Test Topic</item>
</parent-topic>
<body><p>Testing post...</p></body>
<date-added time="14:44" weekday="4">2012-05-03</date-added>
<author>
<item id="1" handle="admin" section-handle="members" section-name="Members">Admin</item>
</author>
</entry>
<entry id="67">
<parent-forum>
<item id="7" handle="general" section-handle="forums" section-name="Forums">General</item>
</parent-forum>
<parent-topic>
<item id="62" handle="test-topic" section-handle="forum-topics" section-name="Forum Topics">Test Topic</item>
</parent-topic>
<body><p>And here's a reply...?</p></body>
<date-added time="22:56" weekday="5">2012-05-04</date-added>
<author>
<item id="1" handle="test-user-1" section-handle="members" section-name="Members">Test User 1</item>
</author>
</entry>
</topic-replies>
<user-list>
<section id="1" handle="members">Members</section>
<entry id="1">
<username handle="admin">Admin</username>
<email>admin@email.com</email>
<role id="2">
<name handle="administrator">Administrator</name>
</role>
</entry>
<entry id="2">
<username handle="test-user-1">Test User 1</username>
<email>test.user.1@email.com</email>
<role id="4">
<name handle="user">User</name>
</role>
</entry>
</user-list>
</data>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="root" select="'rootVariable'"/>
<xsl:variable name="user-list" select="/data/user-list/entry"/>
<xsl:template match="node()|@*">
<xsl:apply-templates select="node()|@*"/>
</xsl:template>
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="topic-replies/entry">
<xsl:variable name="authorEntry" select="$user-list[username/@handle = current()/author/item/@handle]"/>
<ul class="profile">
<li>
<a class="{$authorEntry/role/name/@handle}" href="{$root}/user/{$authorEntry/username/@handle}">
<xsl:value-of select="$authorEntry/username"/>
</a>
</li>
<li>
<xsl:value-of select="$authorEntry/role/name"/>
</li>
</ul>
</xsl:template>
</xsl:stylesheet>
HTML输出
<html>
<ul class="profile">
<li><a class="administrator" href="rootVariable/user/admin">Admin</a></li>
<li>Administrator</li>
</ul>
<ul class="profile">
<li><a class="user" href="rootVariable/user/test-user-1">Test User 1</a></li>
<li>User</li>
</ul>
</html>