2

我有一个使用 xslt 文件打开的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="eventsoverview.xsl"?>
<events>
  <item>
    <date>7/5/2013</date>
    <time>18:00</time>
    <title>Some title</title>
    <location>Earth</location>
    <id>505</id>
    <category>Comedy</category>
    <description>Some description</description>
  </item>
  <item>
    <date>7/5/2013</date>
    <time>18:00</time>
    <title>Some title</title>
    <location>Earth</location>
    <id>506</id>
    <category>Comedy</category>
    <description>Some description</description>
  </item>
  <item>
    <date>7/5/2013</date>
    <time>18:00</time>
    <title>Some title</title>
    <location>Earth</location>
    <id>507</id>
    <category>Comedy</category>
    <description>Some description</description>
  </item>
</events>

事件概述.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <html>
            <body style="background-color:#EFEFEF;">
                <xsl:for-each select="events/item">
                    <ul>
                        <li>
                            <h2 style="color:#9C5D00;">
                                <xsl:value-of select="title"/>
                            </h2>
                            <p>
                                <xsl:value-of select="description"/>
                            </p>
                        </li>
                    </ul>
                    <hr/>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>

现在,这个 xslt 正在显示有关该事件的简要数据。我想要做的是,当我单击事件时,它应该使用相同的 xml 文件显示事件的详细信息。我该怎么做呢?这可以使用多个 xslt 文件来完成吗?

4

3 回答 3

2

在下面的示例中,我假设单击事件会使用id包含事件 ID 的 Get-Parameter 调用页面。您需要一些服务器端逻辑来将此 ID 插入您的 XSL(eventid我的示例中的参数)。当然,您也可以将此参数插入 XML。

如果eventid != 0您看到具有相应 ID 的事件的详细信息。在我的示例中,显示了事件 506 的详细信息。

如果eventid = 0您看到事件列表。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:param name="eventid">506</xsl:param>

    <xsl:template match="/">
        <html>
            <body style="background-color:#EFEFEF;">
                <xsl:choose>
                    <xsl:when test="$eventid = 0">
                        <xsl:apply-templates/>
                    </xsl:when>
                    <xsl:when test="$eventid != 0">
                        <xsl:apply-templates select="/events/item"/>
                    </xsl:when>
                </xsl:choose>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="/events/item">
        <xsl:if test="id/text() = $eventid">
            <div>
                <h2 style="color:#9C5D00;">
                    <xsl:value-of select="title"/>
                </h2>
                <p>Description: <xsl:value-of select="description"/>
                </p>
                <p>Time: <xsl:value-of select="time"/>
                </p>
                <p>Location: <xsl:value-of select="location"/>
                </p>
                <p>ID: <xsl:value-of select="id"/></p>
                <p>Category: <xsl:value-of select="category"/>
                </p>
            </div>
            <hr/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="/events">
        <xsl:for-each select="item">
            <ul>
                <li>
                    <h2 style="color:#9C5D00;">
                        <xsl:value-of select="title"/>
                    </h2>
                    <p>
                        <xsl:value-of select="description"/>
                    </p>
                    <xsl:element name="a">
                        <xsl:attribute name="href">?id=<xsl:value-of select="id"/>
                        </xsl:attribute>more</xsl:element>
                </li>
            </ul>
            <hr/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>
于 2013-05-05T11:22:39.060 回答
2

当您想要做一些比每次都使用相同的 XSLT 呈现相同的 XML 更复杂的事情时,xml-stylesheet处理指令就会失去动力。

您需要查看使用 Javascript API 来调用转换 - 或者更好的是查看Saxon-CE,它允许您编写整个应用程序,包括 XSLT 中的交互和事件处理。

于 2013-05-05T11:47:47.667 回答
0

对于客户端 XSLT,我建议让 XSLT 使用 Javascript 创建 HTML,当点击事件发生时,隐藏分别显示详细信息。

于 2013-05-05T11:20:13.397 回答