删除XML中的元素

时间:2017-01-30 08:02:26

标签: xml xslt

我有这个XML,我需要检查整个XML文件并删除任何具有相同日期,操作和原因的Record_Number。

<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和原因RFL应该保留。

请注意,可能有3个或更多元素具有相同的Date,Action和Reason引用单个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 个答案:

答案 0 :(得分:0)

XSLT-1.0中使用Muenchian分组:

<?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>

XSLT-2.0中使用<for-each-group>

<?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>