2017-01-30 181 views
0

我有這個XML,我需要檢查整個XML文件並刪除具有相同日期,操作和原因的任何Record_Number。刪除XML中的元素

<Data> 
<Row> 
    <Record> 
     <Record_Row> 
      <Record_Number>111</Record_Number> 
      <Date>10242016</Date> 
      <Action>RFL</Action> 
      <Reason>RFL</Reason> 
     </Record_Row> 
    </Record> 
    <Record> 
     <Record_Row> 
      <Record_Number>111</Record_Number> 
      <Date>10242016</Date> 
      <Action>RFL</Action> 
      <Reason>RFL</Reason> 
     </Record_Row> 
    </Record> 
    <Record> 
     <Record_Row> 
      <Record_Number>111</Record_Number> 
      <Date>06302016</Date> 
      <Action>TER</Action> 
      <Reason>ATT</Reason> 
     </Record_Row> 
    </Record> 
    <Record> 
     <Record_Row> 
      <Record_Number>444</Record_Number> 
      <Date>04012016</Date> 
      <Action>HIR</Action> 
      <Reason>REH</Reason> 
     </Record_Row> 
    </Record> 
</Row> 
</Data> 

所以在上面的例子中,只有一個 Record_Number 111日期10242016 和行動重建家庭聯繫和原因RFL應該保持。

注意,有可能是3個以上的元素用相同的日期,操作與理性引用到一個Record_Number,但在我上面的例子我只用2

應該給我在這之後xml文件應用邏輯:

<Data> 
<Row> 
    <Record> 
     <Record_Row> 
      <Record_Number>111</Record_Number> 
      <Date>10242016</Date> 
      <Action>RFL</Action> 
      <Reason>RFL</Reason> 
     </Record_Row> 
    </Record> 
    <Record> 
     <Record_Row> 
      <Record_Number>111</Record_Number> 
      <Date>06302016</Date> 
      <Action>TER</Action> 
      <Reason>ATT</Reason> 
     </Record_Row> 
    </Record> 
    <Record> 
     <Record_Row> 
      <Record_Number>444</Record_Number> 
      <Date>04012016</Date> 
      <Action>HIR</Action> 
      <Reason>REH</Reason> 
     </Record_Row> 
    </Record> 
</Row> 
</Data> 
+1

這是一個經常被問到的問題。請查看其他答案。根據您使用的XSLT版本,您可能需要閱讀Muenchian分組(XSLT1.0)或者(XSLT2.0)。 – Madeyedexter

+0

此外,請發佈您至今已編寫的XSLT代碼。 – Madeyedexter

回答

0

使用Muenchian分組的在XSLT-1.0:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/> 
    <xsl:key name="record-row-key" match="Data/Row/Record/Record_Row" use="concat(Record_Number,Date,Action,Reason)"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="Data/Row/Record"> 
     <xsl:for-each select="Record_Row[generate-id()=generate-id(key('record-row-key',concat(Record_Number,Date,Action,Reason)[1]))]"> 
      <xsl:copy-of select="."/> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

使用<for-each-group>XSLT的2.0:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/"> 
     <Data> 
      <Row> 
       <xsl:for-each-group select="Data/Row/Record" group-by="concat(Record_Row/Record_Number,Record_Row/Date,Record_Row/Action,Record_Row/Reason)"> 
        <xsl:copy-of select="."/> 
       </xsl:for-each-group> 
      </Row> 
     </Data> 
    </xsl:template> 
</xsl:stylesheet>