XSLT删除重复的孩子 [英] XSLT remove duplicate children

查看:138
本文介绍了XSLT删除重复的孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个XSLT转换来取消重复父项的children元素。
在我的情况下,父和孩子都被给予(即我不想重复数据删除任何元素的任何子元素)。

I am looking for an XSLT transformation to de-duplicate the children element of a parent. In my case both parent and children are given (i.e I don't want to deduplicate any children of any element).

例如,我想要重复数据删除< ROWSET>

for example, say I want to deduplicate the <ID> children of <ROWSET>

输入:

<ROWSET>
    <ROW>
         <ID> 1 </ID>
         ...
         <ID> 1 </ID>
         ...
    </ROW>
    <ROW>
         <ID> 2 </ID>
         ...
         <ID> 2 </ID>
         ...
    </ROW>
    ...
</ROWSET>

我希望输出为

<ROWSET>
    <ROW>
         <ID> 1 </ID>
         ...
    </ROW>
    <ROW>
         <ID> 2 </ID>
         ...
    </ROW>
    ...
</ROWSET>

其中...表示存在任何其他标签的数量。

where '...' indicates the presence of any number of any other tags.

编辑:
两个重复的孩子之间可能有任何东西

edit: there may be anything between the two duplicate children

推荐答案

一个简单而直截了当的方法来忽略id,其ID与前一个元素的内容相同。

An easy and straightforward approach to ignore id which have am id with same content as previous element for same parent.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match ="ID" >
        <xsl:if test="not (preceding-sibling::ID/text() = current()/text())" >
            <xsl:copy>
                <xsl:apply-templates select="@* | node()" />
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

这篇关于XSLT删除重复的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆