.net - 如何删除 XML 文件中的重复值并保留最后一个值?

标签 .net python xml xslt duplicates

我需要抑制 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/

相关文章:

mysql - 如何在mysql和Visual Basic中设置变量组?

c# - Azure 将文件从 blob 存储移动到同一存储帐户中的文件存储

python - 如何使用 boto 检查 python (django) 应用程序是否正在 EC2 中运行?

Java SimpleXML 使用类属性填充内部对象

javascript - 使用 Google Apps 脚本对 eBay API 的 XML 请求返​​回 'The API call "GeteBayOfficialTime“无效或在此版本中不受支持”错误

ios - 在 Objective-C 中解析 XML

c# - XPath 版本搜索

c# - TcpListener.AcceptTcpClient 是否抛出不重要的异常?

python - 如何使用 selenium 和 python 单击加载 ajax 数据的链接

python - python中通过正则表达式替换特定字符串后面的数字