XSLT故障排除...尝试更改XML输出

时间:2012-08-16 21:41:31

标签: xml xslt

好的,所以我有这个我试图改变的XML数据。我已经编写了XSLT,但它没有做到这一点,我需要帮助找出它有什么问题。

这是我需要使用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>

这就是我需要的样子:

<?xml version='1.0' encoding='UTF-8'?>
<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>

这是我正在使用的XSLT没有做到这一点:

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

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

<xsl:template match="*">
   <projectedBalanceTotal>
     <xsl:copy-of select="
       ../../bb:projectedBalanceTotal.principalId      |
       ../bb:projectedBalanceTotal.planCodeId          |
       ../bb:projectedBalanceTotal.leaveDescription    |
       ../bb:projected_balance_accrual_amount_for_the_pay_period" />
     <bb:projectedBalanceTotal.effectiveDate>
      <xsl:value-of select="." />
     </bb:projectedBalanceTotal.effectiveDate>
     <xsl:copy-of select="../../bb:projectedBalanceTotal.employeeId" />
   </projectedBalanceTotal>
</xsl:template>

</xsl:stylesheet>

如果我可以移除WD前缀也会很好。任何帮助将不胜感激!!!!

1 个答案:

答案 0 :(得分:0)

在我的样式表中,第一件让我感到震惊的是,在XPath表达式和文字结果元素中,未声明的名称空间前缀bb会多次出现。您声明前缀wd但不使用它;我猜这个命名空间是应该绑定的bb

第二件事是你的XML迫使你的XSLT表现出不自然的行为;如果你对词汇表的设计有任何影响,你应该删除名称projectedBalanceTotal.effectiveDate1等等:删除序列号(不提供任何信息并阻止使用普通的XPath表达式)并重构元素所以没有元素名称必须按照这些方式携带两条信息。

第三件事是,即使修复了命名空间错误,样式表也没有产生输出的原因是模板中select指令的xsl:apply-templates属性与/不匹配。

我希望这会有所帮助。祝你好运。