xslt 中局部区域值的总和

sum out of local area vaules in xslt

我有一个 xml 在根

中有用户和时间
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test_task_2.xsl"?> 
<root>
    <users>
        <user id="1" department="1">
            <name>John Dow</name>
            <phone>+7 (111) 111-11-11</phone>
            <email>test1@test.test</email>
        </user>
        <user id="2" department="1">
            <name>Stan Smith</name>
            <phone>+7 (111) 111-11-22</phone>
            <email>test2@test.test</email>
        </user>
        <user id="3" department="2">
            <name>Homer Simpson</name>
            <phone>+7 (111) 111-11-33</phone>
            <email>test3@test.test</email>
        </user>
        <user id="4" department="3">
            <name>Glen Quagmire</name>
            <phone>+7 (111) 111-11-44</phone>
            <email>test4@test.test</email>
        </user>
        <user id="5" department="2">
            <name>Peter Griffin</name>
            <phone>+7 (111) 111-11-55</phone>
            <email>test5@test.test</email>
        </user>
    </users>
    <hours>
        <hour user="1" hours="10" />
        <hour user="4" hours="8" />
        <hour user="2" hours="7" />
        <hour user="5" hours="5" />
        <hour user="5" hours="13" />
        <hour user="4" hours="11" />
        <hour user="2" hours="2" />
        <hour user="3" hours="3" />
        <hour user="4" hours="1" />
        <hour user="2" hours="1" />
        <hour user="3" hours="6" />
        <hour user="1" hours="9" />
    </hours>
</root>

我需要将其可视化为 table

id name phone email sum of hours
1 John Dow +7(111)-111-11-11 test1@test.test 19

等等

我制作了一个 xsl 打印 ID 名称 phone 和电子邮件

看起来像那样

id name phone email sum of hours
1 John Dow +7(111)-111-11-11 test1@test.test
<?xml version="1.0" encoding="WINDOWS-1251" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
  <body>
        <label>departments</label>
    <table border="1">
      <tr bgcolor="#cccccc">
        <th>id</th>
        <th>name</th>
        <th>phone</th>
        <th>email</th>
       <th>sum of hours</th>
      </tr>
      <xsl:for-each select="root/users/user">
        <tr>
        <td><xsl:value-of select="@id"/></td>
        <td><xsl:value-of select="name"/></td>
        <td><xsl:value-of select="phone"/></td>
        <td><xsl:value-of select="email"/></td>
        <td></td>
        </tr>
      </xsl:for-each>
</table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

但我无法获得小时数

使用key解析cross-references:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="hour" match="hour" use="@user" />

<xsl:template match="/root">
    <html>
        <body>
            <label>departments</label>
            <table border="1">
                <tr bgcolor="#cccccc">
                    <th>id</th>
                    <th>name</th>
                    <th>phone</th>
                    <th>email</th>
                    <th>sum of hours</th>
                </tr>
                <xsl:for-each select="users/user">
                    <tr>
                        <td>
                            <xsl:value-of select="@id"/>
                        </td>
                        <td>
                            <xsl:value-of select="name"/>
                        </td>
                            <td><xsl:value-of select="phone"/>
                        </td>
                        <td>
                            <xsl:value-of select="email"/>
                        </td>
                        <td>
                            <xsl:value-of select="sum(key('hour', @id)/@hours)"/>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>