XSLT - 在这种情况下如何使用分组依据?

发布于 2024-12-05 08:34:58 字数 2558 浏览 1 评论 0原文

此的后续 线程)

我现在实际上需要知道按天分组的访问者数量,然后按时间段分组的访问者数量。我尝试应用 Kirill Polishchuk 建议的分组依据,但我只能将其按某个特定分组元素。现在我实际上需要知道如何应用多个分组依据?例如,首先按“日”分组,然后按期间分组。

我有以下 XML:

<Cinema>
    <Day date="15-09-2011" day="Thursday" week="37" in="543" out="543">
        <Movie name="movie1" in="191" out="191">
            <Period time="16:00:00" in="20" out="20"/>
            <Period time="18:00:00" in="71" out="70"/>
            <Period time="20:00:00" in="100" out="101"/>
        </Movie>
        <Movie name="movie2" in="105" out="105">
            <Period time="16:00:00" in="13" out="13"/>
            <Period time="18:00:00" in="34" out="34"/>
            <Period time="20:00:00" in="58" out="58"/>
        </Movie>
        <Movie name="movie3" in="247" out="247">
            <Period time="16:00:00" in="57" out="57"/>
            <Period time="18:00:00" in="75" out="72"/>
            <Period time="20:00:00" in="115" out="118"/>
        </Movie>
    <Day>
    <Day date="16-09-2011" day="Friday" week="37" in="1151" out="1151">
        <Movie name="movie1" in="364" out="364">
            <Period time="16:00:00" in="106" out="106"/>
            <Period time="18:00:00" in="131" out="129"/>
            <Period time="20:00:00" in="127" out="129"/>
        </Movie>
        <Movie name="movie2" in="333" out="333">
            <Period time="16:00:00" in="89" out="89"/>
            <Period time="18:00:00" in="116" out="116"/>
            <Period time="20:00:00" in="128" out="128"/>
        </Movie>
        <Movie name="movie3" in="454" out="454">
            <Period time="16:00:00" in="104" out="104"/>
            <Period time="18:00:00" in="150" out="150"/>
            <Period time="20:00:00" in="200" out="200"/>
        </Movie>
    <Day>
</Cinema>

我想要获取的是每天每个电影时段的总访客数。例如:

Thursday:
16:00h - in: 90, out: 90
18:00h - in: 180, out: 176
20:00h - in: 273, out: 277
Total - in: 543, out: 543

Friday: 
16:00h - in: 299, out: 299
18:00h - in: 397, out: 395
20:00h - in: 455, out: 457
Total - in: 1151, out: 1151

有人有什么建议吗?提前致谢!

(我目前使用 xslt 1.0,但如果有必要,我可以“升级”到较新的 2.0)

(A follow up of this thread)

I now actually need to know the amount of visiters grouped by Day and then by period. I tried to apply the group-by suggested by Kirill Polishchuk, but I can only get it to group by one certain element. Now I actually need to know how to apply multiple group-by's? E.g. Group by 'Day' first and then by period.

I have the following XML:

<Cinema>
    <Day date="15-09-2011" day="Thursday" week="37" in="543" out="543">
        <Movie name="movie1" in="191" out="191">
            <Period time="16:00:00" in="20" out="20"/>
            <Period time="18:00:00" in="71" out="70"/>
            <Period time="20:00:00" in="100" out="101"/>
        </Movie>
        <Movie name="movie2" in="105" out="105">
            <Period time="16:00:00" in="13" out="13"/>
            <Period time="18:00:00" in="34" out="34"/>
            <Period time="20:00:00" in="58" out="58"/>
        </Movie>
        <Movie name="movie3" in="247" out="247">
            <Period time="16:00:00" in="57" out="57"/>
            <Period time="18:00:00" in="75" out="72"/>
            <Period time="20:00:00" in="115" out="118"/>
        </Movie>
    <Day>
    <Day date="16-09-2011" day="Friday" week="37" in="1151" out="1151">
        <Movie name="movie1" in="364" out="364">
            <Period time="16:00:00" in="106" out="106"/>
            <Period time="18:00:00" in="131" out="129"/>
            <Period time="20:00:00" in="127" out="129"/>
        </Movie>
        <Movie name="movie2" in="333" out="333">
            <Period time="16:00:00" in="89" out="89"/>
            <Period time="18:00:00" in="116" out="116"/>
            <Period time="20:00:00" in="128" out="128"/>
        </Movie>
        <Movie name="movie3" in="454" out="454">
            <Period time="16:00:00" in="104" out="104"/>
            <Period time="18:00:00" in="150" out="150"/>
            <Period time="20:00:00" in="200" out="200"/>
        </Movie>
    <Day>
</Cinema>

What I am trying to get is the total visitors of each movie period per day. For example:

Thursday:
16:00h - in: 90, out: 90
18:00h - in: 180, out: 176
20:00h - in: 273, out: 277
Total - in: 543, out: 543

Friday: 
16:00h - in: 299, out: 299
18:00h - in: 397, out: 395
20:00h - in: 455, out: 457
Total - in: 1151, out: 1151

Does anyone have any suggestions? Thanks in advance!

(I use xslt 1.0 at the moment, but it is possible for me to "upgrade" to the newer 2.0, if necessary)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-12-12 08:34:58
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="k" match="Period" use="concat(../../@date, @time)"/>

    <xsl:template match="/Cinema">
        <xsl:apply-templates select="Day"/>
    </xsl:template>

    <xsl:template match="Day">
        <xsl:value-of select="concat(@day, ':')"/>
        <xsl:text>
</xsl:text>

        <xsl:apply-templates select="*/Period[generate-id(.) = generate-id(key('k', concat(../../@date, @time)))]"/>

        <xsl:value-of select="concat('Total - in: ', @in, ', out: ', @out)"/>
        <xsl:text>

</xsl:text>
    </xsl:template>

    <xsl:template match="Period">
        <xsl:value-of select="
                      concat(substring(@time, 1, 5), 'h - in: ', 
                        sum(key('k', concat(../../@date, @time))/@in), 
                        ', out: ', 
                        sum(key('k', concat(../../@date, @time))/@out))"/>
        <xsl:text>
</xsl:text>
    </xsl:template>

</xsl:stylesheet>

XML:

<Cinema>
    <Day date="15-09-2011" day="Thursday" week="37" in="543" out="543">
        <Movie name="movie1" in="191" out="191">
            <Period time="16:00:00" in="20" out="20"/>
            <Period time="18:00:00" in="71" out="70"/>
            <Period time="20:00:00" in="100" out="101"/>
        </Movie>
        <Movie name="movie2" in="105" out="105">
            <Period time="16:00:00" in="13" out="13"/>
            <Period time="18:00:00" in="34" out="34"/>
            <Period time="20:00:00" in="58" out="58"/>
        </Movie>
        <Movie name="movie3" in="247" out="247">
            <Period time="16:00:00" in="57" out="57"/>
            <Period time="18:00:00" in="75" out="72"/>
            <Period time="20:00:00" in="115" out="118"/>
        </Movie>
    </Day>
    <Day date="16-09-2011" day="Friday" week="37" in="1151" out="1151">
        <Movie name="movie1" in="364" out="364">
            <Period time="16:00:00" in="106" out="106"/>
            <Period time="18:00:00" in="131" out="129"/>
            <Period time="20:00:00" in="127" out="129"/>
        </Movie>
        <Movie name="movie2" in="333" out="333">
            <Period time="16:00:00" in="89" out="89"/>
            <Period time="18:00:00" in="116" out="116"/>
            <Period time="20:00:00" in="128" out="128"/>
        </Movie>
        <Movie name="movie3" in="454" out="454">
            <Period time="16:00:00" in="104" out="104"/>
            <Period time="18:00:00" in="150" out="150"/>
            <Period time="20:00:00" in="200" out="200"/>
        </Movie>
    </Day>
</Cinema>

输出:

Thursday:
16:00h - in: 90, out: 90
18:00h - in: 180, out: 176
20:00h - in: 273, out: 277
Total - in: 543, out: 543

Friday:
16:00h - in: 299, out: 299
18:00h - in: 397, out: 395
20:00h - in: 455, out: 457
Total - in: 1151, out: 1151
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:key name="k" match="Period" use="concat(../../@date, @time)"/>

    <xsl:template match="/Cinema">
        <xsl:apply-templates select="Day"/>
    </xsl:template>

    <xsl:template match="Day">
        <xsl:value-of select="concat(@day, ':')"/>
        <xsl:text>
</xsl:text>

        <xsl:apply-templates select="*/Period[generate-id(.) = generate-id(key('k', concat(../../@date, @time)))]"/>

        <xsl:value-of select="concat('Total - in: ', @in, ', out: ', @out)"/>
        <xsl:text>

</xsl:text>
    </xsl:template>

    <xsl:template match="Period">
        <xsl:value-of select="
                      concat(substring(@time, 1, 5), 'h - in: ', 
                        sum(key('k', concat(../../@date, @time))/@in), 
                        ', out: ', 
                        sum(key('k', concat(../../@date, @time))/@out))"/>
        <xsl:text>
</xsl:text>
    </xsl:template>

</xsl:stylesheet>

XML:

<Cinema>
    <Day date="15-09-2011" day="Thursday" week="37" in="543" out="543">
        <Movie name="movie1" in="191" out="191">
            <Period time="16:00:00" in="20" out="20"/>
            <Period time="18:00:00" in="71" out="70"/>
            <Period time="20:00:00" in="100" out="101"/>
        </Movie>
        <Movie name="movie2" in="105" out="105">
            <Period time="16:00:00" in="13" out="13"/>
            <Period time="18:00:00" in="34" out="34"/>
            <Period time="20:00:00" in="58" out="58"/>
        </Movie>
        <Movie name="movie3" in="247" out="247">
            <Period time="16:00:00" in="57" out="57"/>
            <Period time="18:00:00" in="75" out="72"/>
            <Period time="20:00:00" in="115" out="118"/>
        </Movie>
    </Day>
    <Day date="16-09-2011" day="Friday" week="37" in="1151" out="1151">
        <Movie name="movie1" in="364" out="364">
            <Period time="16:00:00" in="106" out="106"/>
            <Period time="18:00:00" in="131" out="129"/>
            <Period time="20:00:00" in="127" out="129"/>
        </Movie>
        <Movie name="movie2" in="333" out="333">
            <Period time="16:00:00" in="89" out="89"/>
            <Period time="18:00:00" in="116" out="116"/>
            <Period time="20:00:00" in="128" out="128"/>
        </Movie>
        <Movie name="movie3" in="454" out="454">
            <Period time="16:00:00" in="104" out="104"/>
            <Period time="18:00:00" in="150" out="150"/>
            <Period time="20:00:00" in="200" out="200"/>
        </Movie>
    </Day>
</Cinema>

Output:

Thursday:
16:00h - in: 90, out: 90
18:00h - in: 180, out: 176
20:00h - in: 273, out: 277
Total - in: 543, out: 543

Friday:
16:00h - in: 299, out: 299
18:00h - in: 397, out: 395
20:00h - in: 455, out: 457
Total - in: 1151, out: 1151
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文