2012-10-04 99 views
2

我有一個輸入XML,它看起來像這樣,並希望使用XSL轉換如下所示的所需輸出。我一直在瀏覽博客,但找不到與如何刪除與根元素匹配的空標籤而不是子節點的任何相關信息。僅剝離空根元素

<?xml version="1.0" encoding="UTF-8"?> 
<objects xmlns="urn:sobject.partner.soap.sforce.com"> 
    <Revenue__c/> 
    <Revenue__c/> 
    <Revenue__c/> 
    <Revenue__c> 
    <Sales_Org_ID__c>IV</Sales_Org_ID__c> 
    <Branch_ID__c>1</Branch_ID__c> 
    <Branch_Name__c>TEST</Branch_Name__c> 
    <Therapy_Code__c>TEST</Therapy_Code__c> 
    <Therapy_Name__c>TEST</Therapy_Name__c> 
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c> 
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c> 
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c> 
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c> 
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c> 
    <Payor_ID__c>TEST</Payor_ID__c> 
    <Payor_Name__c/> 
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c> 
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c> 
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c> 
    <Account__c>001a000001APU5OAAX</Account__c> 
    <Contact__c>003a000001RL1EFAA1</Contact__c> 
    <Revenue_ID__c>41</Revenue_ID__c> 
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c> 
    </Revenue__c> 
</objects> 

這正是我想要的了:

<?xml version="1.0" encoding="UTF-8"?> 
<objects xmlns="urn:sobject.partner.soap.sforce.com"> 
    <Revenue__c> 
    <Sales_Org_ID__c>IV</Sales_Org_ID__c> 
    <Branch_ID__c>1</Branch_ID__c> 
    <Branch_Name__c>TEST</Branch_Name__c> 
    <Therapy_Code__c>TEST</Therapy_Code__c> 
    <Therapy_Name__c>TEST</Therapy_Name__c> 
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c> 
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c> 
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c> 
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c> 
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c> 
    <Payor_ID__c>TEST</Payor_ID__c> 
    <Payor_Name__c/> 
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c> 
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c> 
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c> 
    <Account__c>001a000001APU5OAAX</Account__c> 
    <Contact__c>003a000001RL1EFAA1</Contact__c> 
    <Revenue_ID__c>41</Revenue_ID__c> 
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c> 
    </Revenue__c> 
</objects> 

任何建議將不勝感激。

+1

要點澄清......空''元素不是「根元素」。 「根」是指文檔的最外層。 ''元素有一個父親'',所以他們不能成爲根。 – LarsH

+0

哦,也許你的意思是「葉元素」,也就是沒有孩子的元素。 (「葉節點」是圖論和CS中的一個通用術語,但不是特別的XML,不像「根節點」。) – LarsH

回答

1

這應該這樣做 - 它僅將apply-templates用於帶子節點的收入節點,然後copy-of複製非空收入樹。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:x="urn:sobject.partner.soap.sforce.com" 
       exclude-result-prefixes="x"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 

    <xsl:template match="/x:objects"> 
    <objects xmlns="urn:sobject.partner.soap.sforce.com"> 
     <xsl:apply-templates select="x:Revenue__c[*]" /> 
    </objects> 
    </xsl:template> 

    <xsl:template match="x:Revenue__c"> 
    <xsl:copy-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 

輸出

<?xml version="1.0" encoding="utf-8"?> 
<objects xmlns="urn:sobject.partner.soap.sforce.com"> 
    <Revenue__c> 
    <Sales_Org_ID__c>IV</Sales_Org_ID__c> 
    <Branch_ID__c>1</Branch_ID__c> 
    <Branch_Name__c>TEST</Branch_Name__c> 
    <Therapy_Code__c>TEST</Therapy_Code__c> 
    <Therapy_Name__c>TEST</Therapy_Name__c> 
    <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c> 
    <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c> 
    <Payor_Type_Name__c>TEST</Payor_Type_Name__c> 
    <Calendar_Year_Number__c>2011</Calendar_Year_Number__c> 
    <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c> 
    <Payor_ID__c>TEST</Payor_ID__c> 
    <Payor_Name__c /> 
    <Payor_Type_Code__c>TEST</Payor_Type_Code__c> 
    <MDM_Account_EID__c>66600001</MDM_Account_EID__c> 
    <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c> 
    <Account__c>001a000001APU5OAAX</Account__c> 
    <Contact__c>003a000001RL1EFAA1</Contact__c> 
    <Revenue_ID__c>41</Revenue_ID__c> 
    <Calendar_Year_Month__c>01</Calendar_Year_Month__c> 
    </Revenue__c> 
</objects> 

編輯 - 它可以簡化爲:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:x="urn:sobject.partner.soap.sforce.com" 
       exclude-result-prefixes="x"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/> 

    <xsl:template match="/x:objects"> 
    <objects xmlns="urn:sobject.partner.soap.sforce.com"> 
     <xsl:copy-of select="x:Revenue__c[*]" /> 
    </objects> 
    </xsl:template> 
</xsl:stylesheet> 
+0

感謝一大堆,這似乎爲我工作....非常感謝。 – Abhi

5

這可能是在同一時間完全是最簡單的/最短的解決方案「推風格「並且是最具擴展性和可維護性的 - 沒有硬編碼的元素名稱,沒有文字結果元素,沒有名稱空間,沒有xsl:copy-of

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="/*/*[not(node())]"/> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔應用:

<objects xmlns="urn:sobject.partner.soap.sforce.com"> 
    <Revenue__c/> 
    <Revenue__c/> 
    <Revenue__c/> 
    <Revenue__c> 
     <Sales_Org_ID__c>IV</Sales_Org_ID__c> 
     <Branch_ID__c>1</Branch_ID__c> 
     <Branch_Name__c>TEST</Branch_Name__c> 
     <Therapy_Code__c>TEST</Therapy_Code__c> 
     <Therapy_Name__c>TEST</Therapy_Name__c> 
     <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c> 
     <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c> 
     <Payor_Type_Name__c>TEST</Payor_Type_Name__c> 
     <Calendar_Year_Number__c>2011</Calendar_Year_Number__c> 
     <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c> 
     <Payor_ID__c>TEST</Payor_ID__c> 
     <Payor_Name__c/> 
     <Payor_Type_Code__c>TEST</Payor_Type_Code__c> 
     <MDM_Account_EID__c>66600001</MDM_Account_EID__c> 
     <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c> 
     <Account__c>001a000001APU5OAAX</Account__c> 
     <Contact__c>003a000001RL1EFAA1</Contact__c> 
     <Revenue_ID__c>41</Revenue_ID__c> 
     <Calendar_Year_Month__c>01</Calendar_Year_Month__c> 
    </Revenue__c> 
</objects> 

有用,正確的結果產生

<objects xmlns="urn:sobject.partner.soap.sforce.com"> 
    <Revenue__c> 
     <Sales_Org_ID__c>IV</Sales_Org_ID__c> 
     <Branch_ID__c>1</Branch_ID__c> 
     <Branch_Name__c>TEST</Branch_Name__c> 
     <Therapy_Code__c>TEST</Therapy_Code__c> 
     <Therapy_Name__c>TEST</Therapy_Name__c> 
     <Therapy_Class_Code__c>TEST</Therapy_Class_Code__c> 
     <Therapy_Class_Name__c>TEST</Therapy_Class_Name__c> 
     <Payor_Type_Name__c>TEST</Payor_Type_Name__c> 
     <Calendar_Year_Number__c>2011</Calendar_Year_Number__c> 
     <Month_Revenue_Amount__c>100.01</Month_Revenue_Amount__c> 
     <Payor_ID__c>TEST</Payor_ID__c> 
     <Payor_Name__c/> 
     <Payor_Type_Code__c>TEST</Payor_Type_Code__c> 
     <MDM_Account_EID__c>66600001</MDM_Account_EID__c> 
     <MDM_Physician_EID__c>99900001</MDM_Physician_EID__c> 
     <Account__c>001a000001APU5OAAX</Account__c> 
     <Contact__c>003a000001RL1EFAA1</Contact__c> 
     <Revenue_ID__c>41</Revenue_ID__c> 
     <Calendar_Year_Month__c>01</Calendar_Year_Month__c> 
    </Revenue__c> 
</objects> 

說明

  1. 身份規則複製「原樣」選擇此模板以供執行的任何節點。

  2. 有一個模板覆蓋了頂部元素的子元素中沒有子元素的任何元素的標識模板。這個模板沒有主體(不產生任何輸出),這有效地「刪除」匹配的節點。

+0

+1,很好的解決方案。爲了完整性,我們可能會指出''模板將會吞下具有屬性的元素(但不包含子元素)。也許這就是OP想要的,如果「空」意味着「沒有孩子」。無論如何,這與給定的輸入數據無關。但是,如果我們決定不想吞下具有屬性的元素:'' – LarsH

+0

@LarsH,是的,是正確的。該解決方案只針對提供的XML文檔。謝謝。 –