我希望按类型对值进行分组,并使用以下XML获取单行中类似Type的grosspay之和。我提供了我想要实现的XML和预期输出的示例数据。
< --- XML数据---->
<Payroll_Data>
<EmpId>1000</EmpId>
<FirstName>Mark</FirstName>
<LastName>Henry</LastName>
<Salary>
<Type>B</Type>
<GrossPay>1000.50</GrossPay>
</Salary>
<Salary>
<Type>B</Type>
<GrossPay>847.50</GrossPay>
</Salary>
<Salary>
<Type>X</Type>
<GrossPay>820</GrossPay>
</Salary>
</Payroll_Data>
<Payroll_Data>
<EmpId>1001</EmpId>
<FirstName>John</FirstName>
<LastName>Diggle</LastName>
<Salary>
<Type>B</Type>
<GrossPay>800.98</GrossPay>
</Salary>
<Salary>
<Type>X</Type>
<GrossPay>630.50</GrossPay>
</Salary>
<Salary>
<Type>X</Type>
<GrossPay>600.50</GrossPay>
</Salary>
</Payroll_Data>
&lt; ----预期结果----&gt;
EmpId FirstName LastName Type GrossPay Sum of Grosspay by Type
1000 Mark Henry B 1000.50 1847.99
1000 Mark Henry B 847.49
1000 Mark Henry X 820 820
1001 John Diggle B 800.98 800.98
1001 John Diggle X 630.50 1231
1001 John Diggle X 600.50
答案 0 :(得分:1)
就像@ ar1991所说,但我认为是:
<xsl:template match="Payroll_Data">
<xsl:for-each-group select="Salary" group-by="Type">
<xsl:for-each select="current-group()">
<xsl:value-of select="../(EmpId, FirstName, LastName), Type, GrossPay,
if (position()=1) then sum(current-group()/GrossPay) else ''"
separator="	"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each-group>
</xsl:template>
答案 1 :(得分:0)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output method="text"/>
<xsl:variable name="comma" select="','"/>
<xsl:template match="/">
<xsl:apply-templates select="//Payroll_Data"/>
</xsl:template>
<xsl:template match="Payroll_Data">
<xsl:for-each-group select="Salary" group-by="Type">
<xsl:value-of select="../(EmpId, FirstName, LastName), Type, GrossPay, sum(current-group()/GrossPay)" separator="	"/>
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>