需要帮助删除文本并通过XSLT替换xml

时间:2012-07-13 17:01:47

标签: xml xslt xslt-1.0

我完全陷入困境。我试图使用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
  4. 我需要用leaveBalanceTotal替换Report_Entry。

    任何帮助将不胜感激!

2 个答案:

答案 0 :(得分: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>

希望有所帮助!如果您有疑问,请告诉我。

答案 1 :(得分: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>