如何使用 XSLT 删除重复的 XML 节点 [英] How to remove duplicate XML nodes using XSLT
问题描述
我有一个很长的 XML 文件,比如
<ele1><child1>context1</child1><child2>test1</child2><child1>context1</child1><ele2><child1>context2</child1><child2>test2</child2><child1>context2</child1><ele3>...........<elen></Root>
现在我想使用 xslt 删除每个
中的所有第二个
,这可能吗?结果是这样的:
<ele1><child1>context1</child1><child2>test1</child2><ele2><child1>context2</child1><child2>test2</child2><ele3>...........<elen></Root>
谢谢你,BR
艾伦
这个问题需要更详细的答案,而不仅仅是指出一个好的 慕尼黑分组来源.
原因是所需的分组需要识别ele[SomeString]"元素的所有子元素及其父元素的名称.这种分组需要定义一个键由两个唯一来源唯一定义,通常通过串联.
这种转变:
<前><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:key name="kElByName" match="*"use="concat(generate-id(..), '+',name())"/><xsl:template match="node()|@*">应用于此 XML 文档时:
<前><根>产生想要的结果:
<前><根>I've got an extremely long XML file, like
<Root>
<ele1>
<child1>context1</child1>
<child2>test1</child2>
<child1>context1</child1>
</ele1>
<ele2>
<child1>context2</child1>
<child2>test2</child2>
<child1>context2</child1>
</ele2>
<ele3>...........<elen>
</Root>
Now I want to remove all the second <child1>
in each <ele>
using xslt, is it possible?
The result would be like this:
<Root>
<ele1>
<child1>context1</child1>
<child2>test1</child2>
</ele1>
<ele2>
<child1>context2</child1>
<child2>test2</child2>
</ele2>
<ele3>...........<elen>
</Root>
Thank u, BR
Allen
This question requires a little bit more detailed answer than just pointing to a good Muenchian Grouping source.
The reason is that the needed grouping requires to identify both the names of all children of an "ele[SomeString]" element and their parent. Such grouping requires to define a key that is uniquely defined by both unique sources, usually via concatenation.
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:key name="kElByName" match="*" use="concat(generate-id(..), '+',name())"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="*[starts-with(name(), 'ele')]"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates select= "*[generate-id() = generate-id(key('kElByName', concat(generate-id(..), '+',name()) )[1]) ]" /> </xsl:copy> </xsl:template> </xsl:stylesheet>
when applied on this XML document:
<Root> <ele1> <child1>context1</child1> <child2>test1</child2> <child1>context1</child1> </ele1> <ele2> <child1>context2</child1> <child2>test2</child2> <child1>context2</child1> </ele2> <ele3> <child2>context2</child2> <child2>test2</child2> <child1>context1</child1> </ele3> </Root>
produces the wanted result:
<Root> <ele1> <child1>context1</child1> <child2>test1</child2> </ele1> <ele2> <child1>context2</child1> <child2>test2</child2> </ele2> <ele3> <child2>context2</child2> <child1>context1</child1> </ele3> </Root>
这篇关于如何使用 XSLT 删除重复的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!