我需要抑制 XML 文件中的所有重复值并保留最终值(引用目标文件)。
请帮忙,因为我不知道是否应该使用 xslt、python 或任何 .NET API
这是源文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
<cd>
<title>old_value</title>
<title>inbetween_value</title>
<title>new_value</title>
</cd>
</catalog>
预期的目标文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
<cd>
<title>new_value</title>
</cd>
</catalog>
最佳答案
有一个极其简单(没有显式条件,没有轴)的 XSLT 解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="ISO-8859-1"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cd/title[not(position() = last())]"/>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时:
<catalog>
<cd>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
<cd>
<title>old_value</title>
<title>inbetween_value</title>
<title>new_value</title>
</cd>
</catalog>
产生了想要的正确结果:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<artist>Bob Dylan</artist>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
<cd>
<title>new_value</title>
</cd>
</catalog>
关于.net - 如何删除 XML 文件中的重复值并保留最后一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9712321/