我是新手,正在编写从XML到CSV的XSLT转换。以下是输出文件的外观(所需结果):
21072,Charles,Bradley,401K,377.96
21072,Charles,Bradley,401K-R,94.49
21082,Alex,Grossman,401K,10657.15
3,11129.6
但是我正在获取此信息,但不确定如何获取计数和总计,还完全排除了401(k)T的行。
21072,Charles,Bradley,401(k) [USA],377.96,
401(k) Roth [USA],94.49,
401(k) T,94.49,
21082,Alex,Grossman,401(k) [USA],10657.15,
401(k) T,10657.15,
我已经在XSLT之后尝试过
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wd="urn:com.workday.report/CR-TestReport"
exclude-result-prefixes="xs wd"
version="2.0">
<xsl:output method="text"/>
<xsl:param name="separator" select="','"/>
<xsl:param name="linefeed" select="' '"/>
<xsl:template match="/">
<xsl:call-template name="DetailRecords"/>
<xsl:call-template name="TrailerRecord"/>
</xsl:template>
<!--Builds Detail Records -->
<xsl:template name="DetailRecords">
<xsl:for-each select = "wd:Report_Data/wd:Report_Entry">
<xsl:value-of select="wd:Employee_ID"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="wd:First_Name"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="wd:Last_Name"/>
<xsl:value-of select="$separator"/>
<xsl:for-each select = "wd:Payroll_Result_Lines">
<xsl:value-of select="wd:Deduction/@wd:Descriptor"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select = "wd:Result_Line_Amount"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="$linefeed"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<!--Builds Trailer Record -->
<xsl:template name="TrailerRecord">
</xsl:template>
</xsl:stylesheet>
此XML文件。
<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data xmlns:wd="urn:com.workday.report/CR-TestReport">
<wd:Report_Entry>
<wd:Employee_ID>21072</wd:Employee_ID>
<wd:Last_Name>Bradley</wd:Last_Name>
<wd:First_Name>Charles</wd:First_Name>
<wd:Payroll_Result_Lines>
<wd:Deduction wd:Descriptor="401(k) [USA]">
<wd:ID wd:type="WID">ff68c0f1227c41a2a5221f6a7e6a67e5</wd:ID>
<wd:ID wd:type="Deduction_Code">401K</wd:ID>
</wd:Deduction>
<wd:Result_Line_Amount>377.96</wd:Result_Line_Amount>
</wd:Payroll_Result_Lines>
<wd:Payroll_Result_Lines>
<wd:Deduction wd:Descriptor="401(k) Roth [USA]">
<wd:ID wd:type="WID">2d277641684a48fb861a896e4eeb3eea</wd:ID>
<wd:ID wd:type="Deduction_Code">401K-R</wd:ID>
</wd:Deduction>
<wd:Result_Line_Amount>94.49</wd:Result_Line_Amount>
</wd:Payroll_Result_Lines>
<wd:Payroll_Result_Lines>
<wd:Deduction wd:Descriptor="401(k) T">
<wd:ID wd:type="WID">2d277641684a48fb861a896e4eeb3eea</wd:ID>
<wd:ID wd:type="Deduction_Code">401K-T</wd:ID>
</wd:Deduction>
<wd:Result_Line_Amount>94.49</wd:Result_Line_Amount>
</wd:Payroll_Result_Lines>
</wd:Report_Entry>
<wd:Report_Entry>
<wd:Employee_ID>21082</wd:Employee_ID>
<wd:Last_Name>Grossman</wd:Last_Name>
<wd:First_Name>Alex</wd:First_Name>
<wd:Payroll_Result_Lines>
<wd:Deduction wd:Descriptor="401(k) [USA]">
<wd:ID wd:type="WID">ff68c0f1227c41a2a5221f6a7e6a67e5</wd:ID>
<wd:ID wd:type="Deduction_Code">401K</wd:ID>
</wd:Deduction>
<wd:Result_Line_Amount>10657.15</wd:Result_Line_Amount>
</wd:Payroll_Result_Lines>
<wd:Payroll_Result_Lines>
<wd:Deduction wd:Descriptor="401(k) T">
<wd:ID wd:type="WID">ff68c0f1227c41a2a5221f6a7e6a67e5</wd:ID>
<wd:ID wd:type="Deduction_Code">401K-T</wd:ID>
</wd:Deduction>
<wd:Result_Line_Amount>10657.15</wd:Result_Line_Amount>
</wd:Payroll_Result_Lines>
</wd:Report_Entry>
</wd:Report_Data>
答案 0 :(得分:1)
在使用XSLT 2创建CSV时,我建议利用xsl:value-of separator
来“构造”一行或在CSV中记录
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xpath-default-namespace="urn:com.workday.report/CR-TestReport"
xmlns:wd="urn:com.workday.report/CR-TestReport"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="text"/>
<xsl:param name="separator" select="','"/>
<xsl:param name="linefeed" select="' '"/>
<xsl:template match="/">
<xsl:variable name="rows" select="//Report_Entry/Payroll_Result_Lines[Deduction[not(ID[@wd:type = 'Deduction_Code'] = '401K-T')]]"/>
<xsl:apply-templates select="$rows"/>
<xsl:value-of select="count($rows), sum($rows/Result_Line_Amount)" separator="{$separator}"/>
</xsl:template>
<xsl:template match="Payroll_Result_Lines">
<xsl:value-of select="../(Employee_ID, First_Name, Last_Name), Deduction/@wd:Descriptor, Result_Line_Amount" separator="{$separator}"/>
<xsl:value-of select="$linefeed"/>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
要获得所需的结果,可以将模板更改为以下内容。
<!--Builds Detail Records -->
<xsl:template name="DetailRecords">
<xsl:for-each select = "wd:Report_Data/wd:Report_Entry">
<xsl:for-each select = "wd:Payroll_Result_Lines[wd:Deduction/wd:ID[@wd:type='Deduction_Code'] != '401K-T']">
<xsl:value-of select="../wd:Employee_ID"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="../wd:First_Name"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="../wd:Last_Name"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="wd:Deduction/wd:ID[@wd:type='Deduction_Code']"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select = "wd:Result_Line_Amount"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="$linefeed"/>
</xsl:for-each>
</xsl:for-each>
<xsl:value-of select="count(wd:Report_Data/wd:Report_Entry/wd:Payroll_Result_Lines[wd:Deduction/wd:ID[@wd:type='Deduction_Code'] != '401K-T'])"/>
<xsl:value-of select="$separator"/>
<xsl:value-of select="sum(wd:Report_Data/wd:Report_Entry/wd:Payroll_Result_Lines[wd:Deduction/wd:ID[@wd:type='Deduction_Code'] != '401K-T']/wd:Result_Line_Amount)"/>
</xsl:template>
其结果是:
21072,Charles,Bradley,401K,377.96,
21072,Charles,Bradley,401K-R,94.49,
21082,Alex,Grossman,401K,10657.15,
3,11129.6
答案 2 :(得分:0)
我无法发表评论,但是除了删除终止分隔符之外,我得到的答案与@ zx485相同。上面的期望结果没有每一行的结尾逗号:
<xsl:value-of select = "wd:Result_Line_Amount"/>
<xsl:value-of select="$linefeed"/>
</xsl:for-each>
我希望这对您有所帮助,并希望与大家分享-我是SO的新手,所以由于zx的答案如此接近,因此不确定最佳的提供方式。