2011-05-30 76 views
6

我有一個XML文檔通過Web服務返回給我。如何使用XLST從XML中刪除某些屬性

<Kronos_WFC encoding="ASCII" version="1.0" WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM"> 
    <Response Status="Success" Timeout="1800" PersonKey="-1" Object="System" Username="1" Action="Logon" PersonNumber="1"> 
    </Response> 
    <Response Status="Success" action="Load"> 
     <ScheduleGroup ScheduleGroupName="SomeName" AllowsInheritance="false" AllowContract="false" IsEmploymentTerm="false" /> 
     <ScheduleGroup ScheduleGroupName="GreatName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" /> 
     <ScheduleGroup ScheduleGroupName="BestName" AllowsInheritance="true" AllowContract="false" IsEmploymentTerm="false" /> 
    </Response> 
    <Response Status="Success" Object="System" Action="Logoff"> 
    </Response> 
</Kronos_WFC> 

的問題是我把成果轉化爲有關本產品(xsd2code)XSD模式生成的業務對象。該產品在架構沒有爲屬性(Response):

  • 超時
  • PersonKey
  • 對象
  • 用戶名

我想做到以下幾點:

  • 刪除上述屬性
  • 將所有其他屬性轉換成元素,包括所有的孩子,和孩子們的兒童等

我如何做到這一點使用XLST。使用Regex刪除不需要的屬性會更簡單嗎?

+0

使用XSLT生成您需要的XML。要開始看到http://www.w3schools.com/xsl – nickytonline 2011-05-30 02:16:36

+0

好問題,+1。查看我的答案,獲得完整,簡短的解決方案和解釋。這個解決方案非常優雅,因爲它使用了最基礎和最強大的XSLT設計模式 - 重寫身份規則/模板。 – 2011-05-30 02:56:08

+0

美麗的+1 Thankyou – Jeremy 2011-05-30 03:57:26

回答

7

使用Regex刪除 不需要的屬性會更簡單嗎?

沒有,這是一個很簡單的XSLT操作:

這種轉變

<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= 
"Response/@*[contains('|Timeout|PersonKey|Object|Username|', 
         concat('|',name(),'|') 
        ) 
      ]"/> 
<xsl:template match="@*"> 
    <xsl:element name="{name()}"> 
    <xsl:value-of select="."/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<Kronos_WFC encoding="ASCII" version="1.0" 
WFCVersion="6.1" TimeStamp="01/5/2011 8:38AM"> 
    <Response Status="Success" Timeout="1800" PersonKey="-1" 
    Object="System" Username="1" Action="Logon" 
    PersonNumber="1"></Response> 
    <Response Status="Success" action="Load"> 
     <ScheduleGroup ScheduleGroupName="SomeName" 
     AllowsInheritance="false" AllowContract="false" 
     IsEmploymentTerm="false" /> 
     <ScheduleGroup ScheduleGroupName="GreatName" 
     AllowsInheritance="true" AllowContract="false" 
     IsEmploymentTerm="false" /> 
     <ScheduleGroup ScheduleGroupName="BestName" 
     AllowsInheritance="true" AllowContract="false" 
     IsEmploymentTerm="false" /> 
    </Response> 
    <Response Status="Success" Object="System" 
    Action="Logoff"></Response> 
</Kronos_WFC> 

PR oduces正是想要的,正確的結果

<Kronos_WFC> 
    <encoding>ASCII</encoding> 
    <version>1.0</version> 
    <WFCVersion>6.1</WFCVersion> 
    <TimeStamp>01/5/2011 8:38AM</TimeStamp> 
    <Response> 
     <Status>Success</Status> 
     <Action>Logon</Action> 
     <PersonNumber>1</PersonNumber> 
    </Response> 
    <Response> 
     <Status>Success</Status> 
     <action>Load</action> 
     <ScheduleGroup> 
     <ScheduleGroupName>SomeName</ScheduleGroupName> 
     <AllowsInheritance>false</AllowsInheritance> 
     <AllowContract>false</AllowContract> 
     <IsEmploymentTerm>false</IsEmploymentTerm> 
     </ScheduleGroup> 
     <ScheduleGroup> 
     <ScheduleGroupName>GreatName</ScheduleGroupName> 
     <AllowsInheritance>true</AllowsInheritance> 
     <AllowContract>false</AllowContract> 
     <IsEmploymentTerm>false</IsEmploymentTerm> 
     </ScheduleGroup> 
     <ScheduleGroup> 
     <ScheduleGroupName>BestName</ScheduleGroupName> 
     <AllowsInheritance>true</AllowsInheritance> 
     <AllowContract>false</AllowContract> 
     <IsEmploymentTerm>false</IsEmploymentTerm> 
     </ScheduleGroup> 
    </Response> 
    <Response> 
     <Status>Success</Status> 
     <Action>Logoff</Action> 
    </Response> 
</Kronos_WFC> 

說明:

  1. 身份規則/模板拷貝的每一個節點 「原樣」。

  2. 覆蓋任何屬性相匹配名稱爲TimeoutPersonKeyObject,或Username具有空體,不復制它們的身份規則模板(「刪除」,從它們的輸出)

  3. 模板匹配任何屬性創建一個元素,其名稱是匹配屬性的名稱,其文本node-child是匹配屬性的值。

記住:使用和壓倒一切的the identity rule是最基本,最強大的XSLT設計模式。

+0

再次謝謝Dimitre! – Jeremy 2011-05-30 03:58:29

+0

@Jeremy孩子:不客氣。 – 2011-05-30 04:07:50

相關問題