在 XSLT 中查找员工的年薪

Finding Annual Salary of Employees in XSLT

您好,我是 XSLT 的新手(习惯于过程式编程语言),但发现很难理解我如何在 XSLT 中完成它,我将不胜感激任何帮助:

xml,我要改造如下- 无非是一个员工的列表,他们的月薪变化。 objective是确定2015年的年薪

    <employees>

        <employee>
            <id>E1</id>
            <hiredt>2000-01-01</hiredt>
            <salaryhistory>
                <change> 
                    <efffrom>2000-01-01</efffrom>
                    <monthlypay>4000</monthlypay>
                </change>
                <change> 
                    <efffrom>2014-01-01</efffrom>
                    <monthlypay>5000</monthlypay>
                </change>
                <change>
                    <efffrom>2015-02-01</efffrom>
                    <monthlypay>6000</monthlypay>
                </change>
                <change>
                    <efffrom>2015-07-01</efffrom>
                    <monthlypay>7000</monthlypay>
                </change>
            </salaryhistory>
        </employee>
        <employee>
            <id>E2</id>
            <hiredt>2015-03-01</hiredt>     
            <salaryhistory>
                <change>
                    <efffrom>2015-03-01</efffrom>
                    <monthlypay>5000</monthlypay>
                </change>
            </salaryhistory>
        </employee>

    </employees>

objective计算所有员工2015年的年薪,转化为如下XML文件

        <employees>

        <employee>
            <id>E1</id>
            <annualsal>77000</annualsal>
        </employee>

        <employee>
            <id>E2</id>
            <annualsal>50000</annualsal>
        </employee>     

    </employees>

计算说明。

Computation for E1
5000 * 1 month  =  5000
6000 * 5 months = 30000
7000 * 6 months = 42000
Total                   77000

E2 的计算

5000 * 10 months = 50,000 employee started on March 1,2015.

任何指导将不胜感激。

可能有更优雅的方式,但是这个works:

XSLT 2.0

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:param name="year" select="2015"/>

<xsl:variable name="months" as="xs:date*">
    <xsl:for-each select="1 to 12">
        <xsl:sequence select="xs:date(concat($year, format-number(., '-00'), '-01'))"/>
    </xsl:for-each>
</xsl:variable>

<xsl:template match="/employees">
    <xsl:copy>
        <xsl:apply-templates select="employee"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="employee">
    <xsl:copy>
        <xsl:copy-of select="id"/>
        <xsl:variable name="salary-changes" select="salaryhistory/change" />
        <xsl:variable name="salaries-by-month" as="xs:integer*">
            <xsl:for-each select="$months">
                <xsl:sequence select="$salary-changes[xs:date(efffrom) le current()][last()]/monthlypay" />
            </xsl:for-each>
        </xsl:variable>
        <annualsal>
            <xsl:value-of select="sum($salaries-by-month)" />
        </annualsal>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

请注意,薪资变化假定按时间顺序列出。