我正在使用 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
模板中?
我想我可能需要使用键,但这将是我第一次使用它们,我真的想不出它背后的逻辑。现在,我真的一点头绪都没有。
提前致谢。