2012-07-13 46 views
1

我完全卡住了。我正在嘗試使用XLST來查找/替換我的xml輸出文件的部分內容。我甚至沒有出發點。我試過使用Altova DiffDog來自動生成我的xlst,因爲我沒有牢牢掌握如何自己編寫它。需要幫助刪除文本並通過XLST替換xlm

這是我目前的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。

任何幫助,將不勝感激!

回答

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

您的第一個模板下的xsl:apply-templates是毫無意義的浪費。這些節點類型永遠不會有子節點。 – 2012-07-13 18:13:10

+0

非常感謝!它像一個魅力!我非常感謝你的時間。 – 2012-07-13 18:38:36

+0

@ LC_123 - 非常歡迎。 – 2012-07-13 18:41:54

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> 

希望有幫助!如果您有任何問題,請告訴我。

+0

你忘記了在過去的3個模板複製屬性。除非運營機構另有規定,否則假設它們可能在那裏,這將是一個更好的解決辦法。由於排序,匹配表達式「node()| @ *」效率較低,「@ * | node()」。最好使用local-name()而不是local-name(。)。 – 2012-07-13 18:24:20

+0

@ SeanB.Durkin - 感謝您的評論;非常感激。我沒有包含屬性,因爲OP的原始問題沒有包含它們。我嘗試回答具體問題,而不是假設沒有明確說明的東西。 – ABach 2012-07-13 23:49:27