0

我正在使用 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-repliesXML 编写 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模板中?

我想我可能需要使用键,但这将是我第一次使用它们,我真的想不出它背后的逻辑。现在,我真的一点头绪都没有。

提前致谢。

4

1 回答 1

1

发生这种情况的原因是您的user-listreply-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>
于 2012-06-05T02:30:27.133 回答