XSL - 删除重复的节点,但保留原始 [英] XSL - remove the duplicate node but keep the original
问题描述
需要使用XSLT从输入xml中删除重复节点的帮助
Need help in removing the duplicate node from the input xml using XSLT
这是我的XML的样子,
This is how my XML looks like,
<?xml version="1.0"?>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"></NodeB>
<NodeC>
<NodeD="ValueD">
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF >
</NodeE >
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF >
</NodeE>
</NodeD>
</NodeC>
</NodeA>
我的最终输出应该像
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"></NodeB>
<NodeC>
<NodeD="ValueD">
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF>
</NodeE >
</NodeD>
</NodeC>
</NodeA>
这里Node E的Name属性具有重复的值。基于这个属性,我需要消除这个重复。
Here the Name attribute of Node E has duplicate values. Based on this attribute I need to eliminate the duplicate.
如果有人可以帮助我使用XSLT来获取输出,这将是非常有帮助的。
我只能使用XSLT 1.0
It would be really helpful if someone could help me with the XSLT required here to get the output. I can only use the XSLT 1.0
推荐答案
如果两个< NodeE>
元素仅当它们具有相同的父代时被认为是重复的,这可能是最简单的解决方案:
If two <NodeE>
elements are considered duplicates only if they have the same parent, this is probably the simplest solution:
<?xml version="1.0"?>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"></NodeB>
<NodeC>
<NodeD Name="ValueD">
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF>
</NodeE>
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF>
</NodeE>
</NodeD>
<!-- Added another <NodeD> element for demonstration -->
<NodeD>
<NodeE Name="ValueABC">
<NodeF Value="0"></NodeF>
</NodeE>
<NodeE Name="ValueDEF">
<NodeF Value="0"></NodeF>
</NodeE>
</NodeD>
</NodeC>
</NodeA>
样式表#1
Stylesheet #1
<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="*"/>
<!--
Identity transform: copy elements and attributes from input file as is
-->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!--
Drop <NodeE> elements with a preceding <NodeE> sibling that has the same
@Name attribute value as the current element
-->
<xsl:template
match="NodeE[preceding-sibling::NodeE[@Name = current()/@Name]]"/>
</xsl:stylesheet>
输出#1
Output #1
<?xml version="1.0" encoding="utf-8"?>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"/>
<NodeC>
<NodeD Name="ValueD">
<NodeE Name="ValueABC">
<NodeF Value="0"/>
</NodeE>
</NodeD>
<NodeD>
<NodeE Name="ValueABC">
<NodeF Value="0"/>
</NodeE>
<NodeE Name="ValueDEF">
<NodeF Value="0"/>
</NodeE>
</NodeD>
</NodeC>
</NodeA>
另一方面,如果< NodeE>
元素应该被视为整个文档中的重复项,您可以使用Muenchian分组:
On the other hand, if <NodeE>
elements should be considered duplicates across the whole document, you can use Muenchian grouping:
<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="kNode" match="NodeE" use="@Name"/>
<!--
Identity transform: copy elements and attributes from input file as is
-->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!--
Use Muenchian grouping to apply unique NodeE elements.
See http://www.jenitennison.com/xslt/grouping/muenchian.html
-->
<xsl:template match="NodeE[generate-id() =
generate-id(key('kNode', @Name)[1])]">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<!-- Drop other <NodeE> elements -->
<xsl:template match="NodeE"/>
</xsl:stylesheet>
输出#2
Output #2
<?xml version="1.0" encoding="utf-8"?>
<NodeA NodeAattr="123">
<NodeB NodeBattr="456"/>
<NodeC>
<NodeD Name="ValueD">
<NodeE Name="ValueABC">
<NodeF Value="0"/>
</NodeE>
</NodeD>
<NodeD>
<NodeE Name="ValueDEF">
<NodeF Value="0"/>
</NodeE>
</NodeD>
</NodeC>
</NodeA>
这篇关于XSL - 删除重复的节点,但保留原始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!