1

我完全被困住了。我正在尝试使用 XLST 查找/替换部分 xml 输出文件。我真的连起点都没有。我尝试使用 Altova DiffDog 来自动生成我的 xlst,因为我对如何自己编写并没有牢牢把握。

这是我当前的 xml 输出文件:

<?xml version='1.0' encoding='UTF-8'?>
<BB:Report_Data xmlns:BB="urn:com.playdate.report/Report-Outbound">
   <BB:Report_Entry>
      <BB:leaveBalanceTotal.employeeId>1234567</BB:leaveBalanceTotal.employeeId>
      <BB:leaveBalanceTotal.bmbId>9280136270</BB:leaveBalanceTotal.bmbId>
      <BB:All_Eligible_Time_Off_Plans_for_Worker>
         <BB:leaveBalanceTotal.leaveCodeId>1010</BB:leaveBalanceTotal.leaveCodeId>
         <BB:leaveBalanceTotal.balanceAmount>0</BB:leaveBalanceTotal.balanceAmount>
         <BB:leaveBalanceTotal.effectiveDate>2012-07-05</BB:leaveBalanceTotal.effectiveDate>
         <BB:leaveBalanceTotal.lastPayEndDate>2012-07-05</BB:leaveBalanceTotal.lastPayEndDate>
      </BB:All_Eligible_Time_Off_Plans_for_Worker>
      <BB:All_Eligible_Time_Off_Plans_for_Worker>
         <BB:leaveBalanceTotal.leaveCodeId>1014</BB:leaveBalanceTotal.leaveCodeId>
         <BB:leaveBalanceTotal.balanceAmount>96</BB:leaveBalanceTotal.balanceAmount>
         <BB:leaveBalanceTotal.effectiveDate>2012-07-05</BB:leaveBalanceTotal.effectiveDate>
         <BB:leaveBalanceTotal.lastPayEndDate>2012-07-05</BB:leaveBalanceTotal.lastPayEndDate>
      </BB:All_Eligible_Time_Off_Plans_for_Worker>
      <BB:leaveBalanceTotal.totalDaysService>1459</BB:leaveBalanceTotal.totalDaysService>
   </BB:Report_Entry>
</BB:Report_Data>

这就是我需要它的样子:

<?xml version='1.0' encoding='UTF-8'?>
<BB:Report_Data xmlns:BB="urn:com.playdate.report/Report-Outbound">
   <leaveBalanceTotal>
      <leaveBalanceTotal.employeeId>1234567</leaveBalanceTotal.employeeId>
      <leaveBalanceTotal.bmbId>9280136270</leaveBalanceTotal.bmbId>
      <leaveBalanceTotal.leaveCodeId>1010</leaveBalanceTotal.leaveCodeId>
      <leaveBalanceTotal.balanceAmount>0</leaveBalanceTotal.balanceAmount>
      <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
      <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
      <leaveBalanceTotal.leaveCodeId>1014</leaveBalanceTotal.leaveCodeId>
      <leaveBalanceTotal.balanceAmount>96</leaveBalanceTotal.balanceAmount>
      <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
      <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
      <leaveBalanceTotal.totalDaysService>1459</leaveBalanceTotal.totalDaysService>
   </leaveBalanceTotal>
</BB:Report_Data>

本质上,我正在尝试删除以下所有出现的情况:

  1. BB:All_Eligible_Time_Off_Plans_for_Worker
  2. /BB:All_Eligible_Time_Off_Plans_for_Worker
  3. BB:

我需要用 leaveBalanceTotal 替换 Report_Entry。

任何帮助,将不胜感激!

4

2 回答 2

0

当这个 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:BB="urn:com.playdate.report/Report-Outbound"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- Identity Template: copies everything as-is -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Remove BB:All_Eligible_Time_Off_Plans_for_Worker elements -->
  <!-- right away, but process their children -->
  <xsl:template match="BB:All_Eligible_Time_Off_Plans_for_Worker" priority="2">
    <xsl:apply-templates /> 
  </xsl:template>

  <!-- Replace BB:Report_Entry elements with leaveBalanceTotal ones -->
  <xsl:template match="BB:Report_Entry">
    <leaveBalanceTotal>
      <xsl:apply-templates />   
    </leaveBalanceTotal>    
  </xsl:template>

  <!-- Remove BB namespace from descendent elements -->
  <xsl:template match="BB:Report_Entry/* | 
      BB:All_Eligible_Time_Off_Plans_for_Worker/*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates />   
    </xsl:element>  
  </xsl:template>

</xsl:stylesheet>

...应用于提供的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<BB:Report_Data xmlns:BB="urn:com.playdate.report/Report-Outbound">
  <BB:Report_Entry>
    <BB:leaveBalanceTotal.employeeId>1234567</BB:leaveBalanceTotal.employeeId>
    <BB:leaveBalanceTotal.bmbId>9280136270</BB:leaveBalanceTotal.bmbId>
    <BB:All_Eligible_Time_Off_Plans_for_Worker>
      <BB:leaveBalanceTotal.leaveCodeId>1010</BB:leaveBalanceTotal.leaveCodeId>
      <BB:leaveBalanceTotal.balanceAmount>0</BB:leaveBalanceTotal.balanceAmount>
      <BB:leaveBalanceTotal.effectiveDate>2012-07-05</BB:leaveBalanceTotal.effectiveDate>
      <BB:leaveBalanceTotal.lastPayEndDate>2012-07-05</BB:leaveBalanceTotal.lastPayEndDate>
    </BB:All_Eligible_Time_Off_Plans_for_Worker>
    <BB:All_Eligible_Time_Off_Plans_for_Worker>
      <BB:leaveBalanceTotal.leaveCodeId>1014</BB:leaveBalanceTotal.leaveCodeId>
      <BB:leaveBalanceTotal.balanceAmount>96</BB:leaveBalanceTotal.balanceAmount>
      <BB:leaveBalanceTotal.effectiveDate>2012-07-05</BB:leaveBalanceTotal.effectiveDate>
      <BB:leaveBalanceTotal.lastPayEndDate>2012-07-05</BB:leaveBalanceTotal.lastPayEndDate>
    </BB:All_Eligible_Time_Off_Plans_for_Worker>
    <BB:leaveBalanceTotal.totalDaysService>1459</BB:leaveBalanceTotal.totalDaysService>
  </BB:Report_Entry>
</BB:Report_Data>

...产生了预期的结果:

<?xml version="1.0" encoding="UTF-8"?>
<BB:Report_Data xmlns:BB="urn:com.playdate.report/Report-Outbound">
  <leaveBalanceTotal>
    <leaveBalanceTotal.employeeId>1234567</leaveBalanceTotal.employeeId>
    <leaveBalanceTotal.bmbId>9280136270</leaveBalanceTotal.bmbId>
    <leaveBalanceTotal.leaveCodeId>1010</leaveBalanceTotal.leaveCodeId>
    <leaveBalanceTotal.balanceAmount>0</leaveBalanceTotal.balanceAmount>
    <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
    <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
    <leaveBalanceTotal.leaveCodeId>1014</leaveBalanceTotal.leaveCodeId>
    <leaveBalanceTotal.balanceAmount>96</leaveBalanceTotal.balanceAmount>
    <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
    <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
    <leaveBalanceTotal.totalDaysService>1459</leaveBalanceTotal.totalDaysService>
  </leaveBalanceTotal>
</BB:Report_Data>

希望有帮助!如果您有任何问题,请告诉我。

于 2012-07-13T17:32:44.003 回答
0

这是另一个 XSLT 1.0 选项。这也将保留数据可能具有的任何属性。

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:BB="urn:com.playdate.report/Report-Outbound" 
    exclude-result-prefixes="BB">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="comment()|processing-instruction()|@*">
        <xsl:copy/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="BB:All_Eligible_Time_Off_Plans_for_Worker">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="BB:Report_Data">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="BB:Report_Entry">
        <leaveBalanceTotal>
            <xsl:apply-templates select="node()|@*"/>
        </leaveBalanceTotal>
    </xsl:template>

</xsl:stylesheet>

输出

<BB:Report_Data xmlns:BB="urn:com.playdate.report/Report-Outbound">
   <leaveBalanceTotal>
      <leaveBalanceTotal.employeeId>1234567</leaveBalanceTotal.employeeId>
      <leaveBalanceTotal.bmbId>9280136270</leaveBalanceTotal.bmbId>
      <leaveBalanceTotal.leaveCodeId>1010</leaveBalanceTotal.leaveCodeId>
      <leaveBalanceTotal.balanceAmount>0</leaveBalanceTotal.balanceAmount>
      <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
      <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
      <leaveBalanceTotal.leaveCodeId>1014</leaveBalanceTotal.leaveCodeId>
      <leaveBalanceTotal.balanceAmount>96</leaveBalanceTotal.balanceAmount>
      <leaveBalanceTotal.effectiveDate>2012-07-05</leaveBalanceTotal.effectiveDate>
      <leaveBalanceTotal.lastPayEndDate>2012-07-05</leaveBalanceTotal.lastPayEndDate>
      <leaveBalanceTotal.totalDaysService>1459</leaveBalanceTotal.totalDaysService>
   </leaveBalanceTotal>
</BB:Report_Data>
于 2012-07-13T17:39:39.823 回答