0

我目前正在使用 xslt 将我的 xml 转换为我可以使用的格式的 xml。我有一个新要求,即以 csv 格式的文件提供转换后的输出 xml。

这是 xslt 之前的 xml:

<?xml version='1.0' encoding='UTF-8'?>
<bb:Report_Data xmlns:bb="urn:com.playdate.report/Absence_Balances-Outbound">
   <bb:Report_Entry>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:All_Eligible_Time_Off_Plans_for_Worker>
         <bb:projectedBalanceTotal.planCodeId>Sick</bb:projectedBalanceTotal.planCodeId>
         <bb:projectedBalanceTotal.leaveDescription>Sick</bb:projectedBalanceTotal.leaveDescription>
         <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      </bb:All_Eligible_Time_Off_Plans_for_Worker>
      <bb:All_Eligible_Time_Off_Plans_for_Worker>
         <bb:projectedBalanceTotal.planCodeId>Vacation</bb:projectedBalanceTotal.planCodeId>
         <bb:projectedBalanceTotal.leaveDescription>Vacation</bb:projectedBalanceTotal.leaveDescription>
         <bb:projected_balance_accrual_amount_for_the_pay_period>3.323077</bb:projected_balance_accrual_amount_for_the_pay_period>
      </bb:All_Eligible_Time_Off_Plans_for_Worker>
      <bb:All_Eligible_Time_Off_Plans_for_Worker>
         <bb:projectedBalanceTotal.planCodeId>Winter Recess</bb:projectedBalanceTotal.planCodeId>
         <bb:projectedBalanceTotal.leaveDescription>Winter Recess</bb:projectedBalanceTotal.leaveDescription>
         <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      </bb:All_Eligible_Time_Off_Plans_for_Worker>
      <bb:projectedBalanceTotal.effectiveDate1>08122012</bb:projectedBalanceTotal.effectiveDate1>
      <bb:projectedBalanceTotal.effectiveDate2>08262012</bb:projectedBalanceTotal.effectiveDate2>
      <bb:projectedBalanceTotal.effectiveDate3>09092012</bb:projectedBalanceTotal.effectiveDate3>
                <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </bb:Report_Entry>

</bb:Report_Data>

这是在 @Tim C 的帮助下使用的 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:bb="urn:com.playdate.report/Absence_Balances-Outbound">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <bb:Report_Data>
         <xsl:apply-templates select="bb:Report_Data/bb:Report_Entry"/>
      </bb:Report_Data>
   </xsl:template>

   <xsl:template match="bb:Report_Entry">
      <xsl:apply-templates select="*[starts-with(local-name(),'projectedBalanceTotal.effectiveDate')]"/>
   </xsl:template>

   <xsl:template match="*[starts-with(local-name(),'projectedBalanceTotal.effectiveDate')]">
      <xsl:apply-templates select="../bb:All_Eligible_Time_Off_Plans_for_Worker">
         <xsl:with-param name="date" select="."/>
      </xsl:apply-templates>
   </xsl:template>

   <xsl:template match="bb:All_Eligible_Time_Off_Plans_for_Worker">
      <xsl:param name="date"/>
      <projectedBalanceTotal>
         <xsl:copy-of select="../bb:projectedBalanceTotal.principalId"/>
         <xsl:copy-of select="*"/>
         <bb:projectedBalanceTotal.effectiveDate>
            <xsl:value-of select="$date"/>
         </bb:projectedBalanceTotal.effectiveDate>
         <xsl:copy-of select="../bb:projectedBalanceTotal.employeeId"/>
      </projectedBalanceTotal>
   </xsl:template>
</xsl:stylesheet>

这是输出:

<?xml version="1.0"?>
<bb:Report_Data xmlns:bb="urn:com.playdate.report/Absence_Balances-Outbound">
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Sick</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Sick</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08122012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Vacation</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Vacation</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>3.323077</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08122012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Winter Recess</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Winter Recess</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08122012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Sick</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Sick</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08262012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Vacation</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Vacation</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>3.323077</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08262012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Winter Recess</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Winter Recess</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>08262012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Sick</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Sick</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>09092012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Vacation</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Vacation</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>3.323077</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>09092012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
   <projectedBalanceTotal>
      <bb:projectedBalanceTotal.principalId>1040182074</bb:projectedBalanceTotal.principalId>
      <bb:projectedBalanceTotal.planCodeId>Winter Recess</bb:projectedBalanceTotal.planCodeId>
      <bb:projectedBalanceTotal.leaveDescription>Winter Recess</bb:projectedBalanceTotal.leaveDescription>
      <bb:projected_balance_accrual_amount_for_the_pay_period>0</bb:projected_balance_accrual_amount_for_the_pay_period>
      <bb:projectedBalanceTotal.effectiveDate>09092012</bb:projectedBalanceTotal.effectiveDate>
      <bb:projectedBalanceTotal.employeeId>0260971</bb:projectedBalanceTotal.employeeId>
   </projectedBalanceTotal>
</bb:Report_Data>

我有一个紧急要求,需要 csv 格式的每条记录,例如:

projectedBalanceTotal.principalId, projectedBalanceTotal.planCodeId, projectedBalanceTotal.leaveDescription, projected_balance_accrual_amount_for_the_pay_period, projectedBalanceTotal.effectiveDate, projectedBalanceTotal.employeeId
1040182074, Sick, Sick, 0, 08122012, 0260971
1040182074, Vacation, Vacation, 3.323077, 0260971
1040182074, Winter Recess, Winter Recess, 0, 0260971

等等等等

我可以在这个模板上堆叠另一个模板来完成这个吗?

4

1 回答 1

0

我可以在这个模板上堆叠另一个模板来完成这个吗?

是的。但是“怎么样?” 是一个稍微棘手的问题。

如果您可以使用 XSLT 2.0,只需从现有模板中获取输出,并将其视为另一个输出 CSV 模板的输入文档。

如果您坚持使用 XSLT 1.0,则需要使用 nodeset() 扩展(非标准,但在 XSLT 实现中很常见)。使用此函数将现有模板的输出从结果树片段 (RTF) 转换为节点集,以便新模板可以使用它来将其转换为 CSV。

更新

好的,所以将 XSLT 版本 =“1.0”更改为“2.0”。现在将输出方法从“xml”更改为“text”。

然后修改 match="/" 模板说:

<xsl:template match="/">
   <xsl:variable name="reportData">
      <bb:Report_Data>
         <xsl:apply-templates select="bb:Report_Data/bb:Report_Entry"/>
      </bb:Report_Data>
   </xsl:variable>
   <xsl:text>projectedBalanceTotal.principalId, projectedBalanceTotal.planCodeId, projectedBalanceTotal.leaveDescription, projected_balance_accrual_amount_for_the_pay_period, projectedBalanceTotal.effectiveDate, projectedBalanceTotal.employeeId</xsl:text>
   <xsl:apply-templates select="$reportData//projectedBalanceTotal" mode="xml2csv" />
</xsl:template>

然后添加一个模板将 a<projectedBalanceTotal>转换为 CSV 记录:

<xsl:template match="projectedBalanceTotal" mode="xml2csv">
   <xsl:value-of select="bb:projectedBalanceTotal.principalId" />
   <xsl:text>, </xsl:text>
   ...
   <xsl:text>&#10;</xsl:text> <!-- end of line -->
</xsl:template>
于 2012-08-23T04:07:53.750 回答