【问题标题】:xslt remove duplicate and original elementxslt 删除重复和原始元素
【发布时间】:2014-08-04 15:28:15
【问题描述】:

我有以下 XML 并想删除“TxnId”和“OrigTxnId”相同的两条记录。

<XML>
<Record><GroupId>10028</GroupId><SessionId>264-10028-1-515530-2</SessionId><TxnId>264-10028-1-515539-1</TxnId><Date>31-Jul-2014</Date><Time>11:22:40</Time><Account>1111111111</Account><NAD>5000</NAD></Record>
<Record><GroupId>10028</GroupId><SessionId>264-10028-1-515530-2</SessionId><TxnId>264-10028-1-515539-2</TxnId><Date>31-Jul-2014</Date><Time>11:22:40</Time><Account>2222222222</Account><NAD>6000</NAD></Record>
<Record><GroupId>10028</GroupId><SessionId>264-10028-1-515545-1</SessionId><TxnId>264-10028-1-515545-2</TxnId><Date>31-Jul-2014</Date><Time>11:22:55</Time><Account>3333333333</Account><NAD>1000</NAD></Record>
<Record><GroupId>10028</GroupId><TxnId>264-10028-1-515550-1</TxnId><Date>31-Jul-2014</Date><Time>11:23:32</Time><OrigTxnId>264-10028-1-515545-2</OrigTxnId><Account>3333333333</Account><NAD>1000</NAD></Record>
</XML>

这是我当前的 XSL,但它仍然提供原始记录。

<xsl:transform 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="Record">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="/XML">
        <XML>
            <xsl:apply-templates select="Record[not(TxnId=preceding-sibling::Record/OrigTxnId or OrigTxnId=preceding-sibling::Record/TxnId and Account=preceding-sibling::Record/Account)]"/>
        </XML>
    </xsl:template>
</xsl:transform>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    怎么样:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:key name="original" match="Record" use="TxnId" />
    <xsl:key name="copy" match="Record" use="OrigTxnId" />
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Record[key('original', OrigTxnId) or key('copy', TxnId)]"/>
    
    </xsl:stylesheet>
    

    【讨论】:

    • @Chris 如果您的问题得到解答,请通过接受答案关闭它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多