XSLT - XML 中每个元素中每个属性的总和与 for-each?

发布于 2024-12-05 01:47:11 字数 1131 浏览 1 评论 0原文

我对整个 XSLT 事物很陌生,并且我已经尝试通过几个论坛查找它,但我仍然没有找到解决我的问题的实际解决方案。

我有以下 XML:

<Cinema>
    <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>
</Cinema>

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

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

我尝试为每个循环嵌套,但我无法真正弄清楚如何在这种示例中使用它,因为 XSLT 不接受可更改的变量,而我实际上已经习惯了(过程编程)。

有人能为我解决这个问题吗?提前致谢!

I am new to the whole XSLT thing and I've already tried looking it up through several forums, but I still haven't found the actual solution to my problem.

I have the following XML:

<Cinema>
    <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>
</Cinema>

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

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

I tried nested for each loops but I couldn't really figure out how to use it in this kind of example, because XSLT doesn't accept changeable variables, which I am actually used to (procedural programming).

Does anyone have a simple solution to this problem for me? Thanks in advance!

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

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

发布评论

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

评论(1

十级心震 2024-12-12 01:47:11

您可以使用 sum 函数。

XSTL 1.0 解决方案:

<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="@time"/>

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

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

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

</xsl:stylesheet>

输出:

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

它使用 Muenchian 方法进行分组。参考:http://www.jenitennison.com/xslt/grouping/muenchian.html

// 是 /descendant-or-self::node()/ 的缩写。例如, //para 是
/descendant-or-self::node()/child::para 的缩写,因此将选择
文档中的任何 para 元素(甚至是一个 para 元素
文档元素将由 //para 选择,因为文档元素
节点是根节点的子节点); div//para 的缩写
div/descendant-or-self::node()/child::para 等将选择所有 para
div 子级的后代。

参考:http://www.w3.org/TR/xpath/#path-abbrev

You can use sum function.

XSTL 1.0 solution:

<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="@time"/>

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

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

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

</xsl:stylesheet>

Output:

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

It uses Muenchian method for grouping. Reference: http://www.jenitennison.com/xslt/grouping/muenchian.html

// is short for /descendant-or-self::node()/. For example, //para is
short for /descendant-or-self::node()/child::para and so will select
any para element in the document (even a para element that is a
document element will be selected by //para since the document element
node is a child of the root node); div//para is short for
div/descendant-or-self::node()/child::para and so will select all para
descendants of div children.

Reference: http://www.w3.org/TR/xpath/#path-abbrev

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文