使用 xsl 从 xml 文件中删除重复节点 [英] remove duplicate nodes from xml file using xsl
问题描述
我正在寻找一种解决方案来从不基于精确节点名称的 XML 文件中删除所有重复项,相反,我正在寻找一种可以识别所有重复节点并删除它们的解决方案.只有第一个节点应该存在,其余的重复节点将被删除.
I am looking for a solution to delete all duplicates from an XML file not based on Exact node name, Instead, I am looking for a solution that can identify all the duplicate nodes and delete them. Only the first node should exist, and the rest of the duplicate nodes to be deleted.
我读了几篇类似的帖子:
I read couple of similar posts:
示例:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
<project id="staticproperties">
<property name="prop1">removing this prop if its duplicate</property>
<property name="prop2">removing this prop if its duplicate</property>
<property name="prop3">removing this prop if its duplicate</property>
<property name="prop4">removing this prop if its duplicate</property>
</project>
<project id="febrelease2013">
<property name="prop">testing prop from pom.xml</property>
<property name="prop1">removing this prop if its duplicate</property>
<property name="prop3">removing this prop if its duplicate</property>
<property name="prop1">removing this prop if its duplicate</property>
<property name="prop5">removing this prop if its duplicate</property>
</project>
</projects>
注意:
所需的输出是:
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<projects>
<project id="staticproperties">
<property name="prop1">removing this prop if its duplicate</property>
<property name="prop2">removing this prop if its duplicate</property>
<property name="prop3">removing this prop if its duplicate</property>
<property name="prop4">removing this prop if its duplicate</property>
</project>
<project id="febrelease2013">
<property name="prop">testing prop from pom.xml</property>
<property name="prop5">removing this prop if its duplicate</property>
</project>
</projects>
推荐答案
通过 XSLT 1.0 做到这一点的一种方法是利用 慕尼黑分组方法,仅输出唯一的
元素(基于它们的 @name
属性).
One way to do this via XSLT 1.0 is by utilizing the Muenchian Grouping methodology to output only unique <property>
elements (based on their @name
attribute).
例如,当这个 XSLT:
For example, when this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kPropertyByName" match="property" use="@name"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template
match="property[
not(
generate-id() =
generate-id(key('kPropertyByName', @name)[1])
)
]"/>
</xsl:stylesheet>
...应用于提供的 XML,产生想要的结果:
...is applied against the provided XML, the wanted result is produced:
<?xml version="1.0" encoding="UTF-8"?>
<projects>
<project id="staticproperties">
<property name="prop1">removing this prop if its duplicate</property>
<property name="prop2">removing this prop if its duplicate</property>
<property name="prop3">removing this prop if its duplicate</property>
<property name="prop4">removing this prop if its duplicate</property>
</project>
<project id="febrelease2013">
<property name="prop">testing prop from pom.xml</property>
<property name="prop5">removing this prop if its duplicate</property>
</project>
</projects>
这篇关于使用 xsl 从 xml 文件中删除重复节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!