2011-07-18 137 views
4

我有以下輸入XML這需要使用XSLTXSLT:基於名稱屬性刪除重複的節點

輸入XML被轉化:

<result> 
    <circuit>MX123456</circuit> 
    <psaresult> 
     <Live_Alarms> 
      <diagnosis> 
       <utr>xxx</utr> 
       <dtr>xxx</dtr> 
       <text /> 
       <site>xxx</site> 
       <address /> 
       <tech_type /> 
       <end /> 
       <network_type>xxx</network_type> 
      </diagnosis> 
      <Alarms> 
       <alarmId>463</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:21</eventTime> 
       <clearTime /> 
       <problemCode>xxxx</problemCode> 
       <problemText>xxxx</problemText> 
       <equipmentName>xxx</equipmentName> 
       <unit>xxx</unit> 
       <rcKey>xxx</rcKey> 
       <category>xxx</category> 
       <rootCause>xxxx</rootCause> 
      </Alarms> 
      <Alarms> 
       <alarmId>464</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:22</eventTime> 
       <clearTime /> 
       <problemCode>yyyy</problemCode> 
       <problemText>yyyy</problemText> 
       <equipmentName>yyyy</equipmentName> 
       <unit>yyyy</unit> 
       <rcKey>yyyy</rcKey> 
       <category>yyyy</category> 
       <rootCause>yyyy</rootCause> 
      </Alarms> 
     </Live_Alarms> 
    </psaresult> 
</result> 

預期輸出:

<result> 
    <circuit>MX123456</circuit> 
    <psaresult> 
     <Live_Alarms> 
      <psadiagnosis> 
       <utr>xxx</utr> 
       <dtr>xxx</dtr> 
       <text /> 
       <site>xxx</site> 
       <address /> 
       <tech_type /> 
       <end /> 
       <network_type>xxx</network_type> 
      </psadiagnosis> 
      <Alarms> 
       <alarmId>463</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:21</eventTime> 
       <clearTime /> 
       <problemCode>xxxx</problemCode> 
       <problemText>xxxx</problemText> 
       <equipmentName>xxx</equipmentName> 
       <unit>xxx</unit> 
       <rcKey>xxx</rcKey> 
       <category>xxx</category> 
       <rootCause>xxxx</rootCause> 
      </Alarms> 
     </Live_Alarms> 
    </psaresult> 
</result> 

注意:Ony第一個​​節點需要像什麼SelectSinglenode做的和所有其他需要被截斷fr輸出。你可以請建議如何從xslt實現這一點?

+0

好問題,+1。請參閱我的回答,以獲取基於最基本且功能強大的XSLT設計模式的完整,簡短易用的解決方案 - 重寫身份規則。 –

+0

也提供解釋和鏈接。 –

回答

0

使用此:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> 
    <xsl:output method="xml" indent="yes"/> 

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

</xsl:stylesheet> 

輸出XML:

<?xml version="1.0" encoding="utf-8"?> 
<result> 
    <circuit>MX123456</circuit> 
    <psaresult> 
     <Live_Alarms> 
      <diagnosis> 
       <utr>xxx</utr> 
       <dtr>xxx</dtr> 
       <text /> 
       <site>xxx</site> 
       <address /> 
       <tech_type /> 
       <end /> 
       <network_type>xxx</network_type> 
      </diagnosis> 
      <Alarms> 
       <alarmId>463</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:21</eventTime> 
       <clearTime /> 
       <problemCode>xxxx</problemCode> 
       <problemText>xxxx</problemText> 
       <equipmentName>xxx</equipmentName> 
       <unit>xxx</unit> 
       <rcKey>xxx</rcKey> 
       <category>xxx</category> 
       <rootCause>xxxx</rootCause> 
      </Alarms> 


     </Live_Alarms> 
    </psaresult> 

</result> 
+0

我碰巧使用這個,它工作。謝謝。對此的任何其他意見是否需要修改? – Jaisal

+0

@Jaisal,它將複製第1個「報警」節點。 –

2

這裏是典型的身份規則 - 基於解決方案

<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="Alarms[position() >1]"/> 
</xsl:stylesheet> 

當所提供的XML應用文件

<result> 
    <circuit>MX123456</circuit> 
    <psaresult> 
     <Live_Alarms> 
      <diagnosis> 
       <utr>xxx</utr> 
       <dtr>xxx</dtr> 
       <text /> 
       <site>xxx</site> 
       <address /> 
       <tech_type /> 
       <end /> 
       <network_type>xxx</network_type> 
      </diagnosis> 
      <Alarms> 
       <alarmId>463</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:21</eventTime> 
       <clearTime /> 
       <problemCode>xxxx</problemCode> 
       <problemText>xxxx</problemText> 
       <equipmentName>xxx</equipmentName> 
       <unit>xxx</unit> 
       <rcKey>xxx</rcKey> 
       <category>xxx</category> 
       <rootCause>xxxx</rootCause> 
      </Alarms> 
      <Alarms> 
       <alarmId>464</alarmId> 
       <cct>xxx</cct> 
       <eventTime>12/05/11 09:21:22</eventTime> 
       <clearTime /> 
       <problemCode>yyyy</problemCode> 
       <problemText>yyyy</problemText> 
       <equipmentName>yyyy</equipmentName> 
       <unit>yyyy</unit> 
       <rcKey>yyyy</rcKey> 
       <category>yyyy</category> 
       <rootCause>yyyy</rootCause> 
      </Alarms> 
     </Live_Alarms> 
    </psaresult> 
</result> 

完全想要的,正確的結果產生

<result> 
    <circuit>MX123456</circuit> 
    <psaresult> 
     <Live_Alarms> 
     <diagnosis> 
      <utr>xxx</utr> 
      <dtr>xxx</dtr> 
      <text/> 
      <site>xxx</site> 
      <address/> 
      <tech_type/> 
      <end/> 
      <network_type>xxx</network_type> 
     </diagnosis> 
     <Alarms> 
      <alarmId>463</alarmId> 
      <cct>xxx</cct> 
      <eventTime>12/05/11 09:21:21</eventTime> 
      <clearTime/> 
      <problemCode>xxxx</problemCode> 
      <problemText>xxxx</problemText> 
      <equipmentName>xxx</equipmentName> 
      <unit>xxx</unit> 
      <rcKey>xxx</rcKey> 
      <category>xxx</category> 
      <rootCause>xxxx</rootCause> 
     </Alarms> 
     </Live_Alarms> 
    </psaresult> 
</result> 

說明

  1. identity rule(模板)副本的每個節點「現況」。

  2. 有一個模板覆蓋身份規則。它匹配不是其父項的第一個子項的任何​​元素。這個模板沒有主體 - 有效地丟棄任何這樣的匹配元素被複制到輸出中。