使用 XSLT 删除连续的重复项 [英] Removing consecutive duplicates with XSLT
问题描述
我有一些 XML,我想在其中删除相同的连续子节点,这些子节点位于不同的父节点中.也就是说,如果一个子节点(在不同的父节点中)我的 XML 树连续出现两次或更多次,我想删除所有重复项.
I have some XML where I would like to remove identical consecutive child nodes, which are in different parents. That is, if a child (in different parents) node my XML tree appears two times or more consecutively, I want to remove all the duplicates.
我想到的重复节点是前两个
节点中的
.
The duplicate nodes I'm thinking of are the <child>a</child>
in the first two <parent>
nodes.
示例:
这是源 XML:
<root>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
<parent>
<child>a</child>
<child>bb</child>
<child>cc</child>
</parent>
<parent>
<child>aaa</child>
<child>bbb</child>
<child>ccc</child>
</parent>
<parent>
<child>a</child>
<child>bbbb</child>
<child>cccc</child>
</parent>
</root>
这是所需的 XML:
<root>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
<parent>
<child>bb</child>
<child>cc</child>
</parent>
<parent>
<child>aaa</child>
<child>bbb</child>
<child>ccc</child>
</parent>
<parent>
<child>a</child>
<child>bbbb</child>
<child>cccc</child>
</parent>
</root>
仅删除一个元素,但如果有,例如,开头有 5 个连续的
节点(而不是 2 个),则将删除其中的四个.我使用的是 XSLT 2.0.
Only one element is removed but if there were, for example, 5 consecutive <child>a</child>
nodes at the beginning (instead of 2), four of them would be removed. I'm using XSLT 2.0.
感谢您的帮助.
跟进:
感谢 Kirill,我得到了我想要的文档,但是如果我有这样的 XML 文档,这就产生了一个我没有预料到的新问题:
Thanks to Kirill I get the documents I want, however this has spawned a new problem that I didn't anticipate, if I have an XML document like this:
<root>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
<parent>
<child>aaa</child>
<child>bbb</child>
<child>ccc</child>
</parent>
</root>
我应用了 Kirill 的 XSLT,我明白了:
And I apply Kirill's XSLT, I get this:
<root>
<parent>
<child>a</child>
<child>b</child>
<child>c</child>
</parent>
<parent>
</parent>
<parent>
<child>aaa</child>
<child>bbb</child>
<child>ccc</child>
</parent>
</root>
我怎样才能同时删除 <parent></parent>
?对于我的应用程序,可能还有
的其他子元素,如果
元素.
元素,则可以删除这些子元素;
How can I also remove the <parent> </parent>
? For my application there may be other subelements of <parent>
, which are OK to remove if there is no <child>
element in the <parent>
element.
我不喜欢的一个解决方案是在第一个转换之后应用另一个转换.这仅在按顺序应用时才有效,我需要一个单独的 XSLT 文件,并且需要运行两个命令而不是一个.
A solution I have, that I don't like, is to apply another transform after the first one. This only works when applied in order though and I need a separate XSLT file and need to run two commands instead of one.
这是:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="parent[not(child)]"/>
推荐答案
使用:
<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()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="child[../preceding-sibling::parent[1]/child = .]"/>
</xsl:stylesheet>
这篇关于使用 XSLT 删除连续的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!