XSLT - XML中的每个元素中的每个属性与for-each的和? [英] XSLT - Sum of each attribute in every element in XML with for-each?

查看:142
本文介绍了XSLT - XML中的每个元素中的每个属性与for-each的和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是整个XSLT的新手,我已经试过通过几个论坛来查找,但是我还没有找到解决我的问题的实际解决方案。



我有以下XML:

 < Cinema> 
< Movie name =movie1in =191out =191>
< Period time =16:00:00in =20out =20/>
< Period time =18:00:00in =71out =70/>
< Period time =20:00:00in =100out =101/>
< / Movie>
< Movie name =movie2in =105out =105>
< Period time =16:00:00in =13out =13/>
< Period time =18:00:00in =34out =34/>
< Period time =20:00:00in =58out =58/>
< / Movie>
< Movie name =movie3in =247out =247>
< Period time =16:00:00in =57out =57/>
< Period time =18:00:00in =75out =72/>
< Period time =20:00:00in =115out =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不接受可变变量,我实际上习惯于(过程式编程)。

有没有人对我这个问题有一个简单的解决方案?提前感谢!

解决方案您可以使用 sum 函数。 p>

XSTL 1.0解决方案:

 < xsl:stylesheet version =1.0 xmlns:xsl =http://www.w3.org/1999/XSL/Transform> 
< xsl:output omit-xml-declaration =yesindent =yes/>

< xsl:key name =kmatch =Perioduse =@ time/>

< xsl:template match =/ Cinema>

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

< xsl:template match =期间>
concat(substring(@time,1,5),'h - in:',
sum(key('k',@时间)/ @ in),
',out:',
sum(key('k',@time)/ @ out))/>
< xsl:text>&#xA;< / xsl:text>
< / xsl:template>

< / xsl:stylesheet>

输出:

  16:00h  -  in:90,out:90 
18:00h - in:180,out:176
20:00h - in:273,out:277
总数在: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的缩写,所以会选择div子元素的所有para
后代。

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


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!

解决方案

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>&#xA;</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

这篇关于XSLT - XML中的每个元素中的每个属性与for-each的和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆