您好我对XSLT(用于过程编程语言)相对较新,但发现很难理解如何在XSLT中实现这一点并且会感谢任何帮助:
xml,我想转换如下 - 只不过是员工月薪变化的列表。目标是确定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>
目标是计算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.
非常感谢任何指导。
答案 0 :(得分:0)
可能有一种更优雅的方式,但是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>
请注意,假设工资变动按时间顺序列出。