Home > Software design >  sum out of local area vaules in xslt
sum out of local area vaules in xslt

Time:05-24

i have an xml with users and hours in the root

<?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>[email protected]</email>
        </user>
        <user id="2" department="1">
            <name>Stan Smith</name>
            <phone> 7 (111) 111-11-22</phone>
            <email>[email protected]</email>
        </user>
        <user id="3" department="2">
            <name>Homer Simpson</name>
            <phone> 7 (111) 111-11-33</phone>
            <email>[email protected]</email>
        </user>
        <user id="4" department="3">
            <name>Glen Quagmire</name>
            <phone> 7 (111) 111-11-44</phone>
            <email>[email protected]</email>
        </user>
        <user id="5" department="2">
            <name>Peter Griffin</name>
            <phone> 7 (111) 111-11-55</phone>
            <email>[email protected]</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>

and i need to visualize it as a table

id name phone email sum of hours
1 John Dow 7(111)-111-11-11 [email protected] 19

and so on

i have made an xsl which do print id name phone and email

so it looks like that

id name phone email sum of hours
1 John Dow 7(111)-111-11-11 [email protected]
<?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>

but i cant get sum of hours

CodePudding user response:

Use a key to resolve 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>
  • Related