0

好的,所以我有我正在尝试更改的 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 前缀也很好。任何帮助将不胜感激!!!!

4

2 回答 2

0

在查看您的样式表时,首先让我印象深刻的是,未声明的名称空间前缀bb在 XPath 表达式和文字结果元素中多次出现。您声明了前缀wd但不使用它;我想命名空间是bb应该绑定的。

第二件事是您的 XML 迫使您的 XSLT 执行不自然的行为;如果你对词汇表的设计有任何影响,你应该去掉名字projectedBalanceTotal.effectiveDate1等等:去掉序列号(它不提供任何信息并阻止使用正常的 XPath 表达式)并重组元素,所以没有元素名以这些方式携带两条信息。

第三件事是即使在命名空间错误被修复后样式表也没有输出的原因是模板中指令select上的属性xsl:apply-templates/ 不匹配任何东西。

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

于 2012-08-17T04:17:46.887 回答
0

我认为您可能首先需要从匹配bb:Report_Entry元素开始

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

然后,对于每个这样的条目,您尝试匹配生效日期

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

然后,在匹配日期的模板中,匹配bb:All_Eligible_Time_Off_Plans_for_Worker元素,但将日期作为参数传入。

<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>

bb:All_Eligible_Time_Off_Plans_for_Worker的模板然后可以输出 in 中的元素,以及作为参数传递的有效。

这是完整的 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 时,将输出以下内容

<?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>

我不完全确定您是否真的想更改名称空间,所以目前 XSLT 保持不变。

于 2012-08-17T08:18:12.710 回答