xml - 使用 xslt 从 xml 文件中删除基于条件的重复记录
问题描述
如果输入 xml 中出现重复Emplid的记录,那么我想删除状态为“已撤回”的记录。如果具有相同 emplid 的记录出现两次,我只想保留状态为活动的记录。
输入xml
<Recordset>
<Record>
<Emplid>10001</Emplid>
<name>Bob Dylan</name>
<country>USA</country>
<company>Columbia</company>
<status>active</status>
<year>1985</year>
</Record>
<Record>
<Emplid>10002</Emplid>
<name>Bonnie Tyler</name>
<country>UK</country>
<company>CBS Records</company>
<status>withdrawn</status>
<year>1988</year>
</Record>
<Record>
<Emplid>10001</Emplid>
<name>Bob Dylan</name>
<country>Uk</country>
<company>CBS Records</company>
<status>withdrwan</status>
<year>1975</year>
</Record>
</Recordset>
预期的 xml
Recordset>
<Record>
<Emplid>10001</Emplid>
<name>Bob Dylan</name>
<country>USA</country>
<company>Columbia</company>
<status>active</status>
<year>1985</year>
</Record>
<Record>
<Emplid>10002</Emplid>
<name>Bonnie Tyler</name>
<country>UK</country>
<company>CBS Records</company>
<status>withdrawn</status>
<year>1988</year>
</Record>
</Recordset>
感谢有人可以帮助我。
解决方案
所以基本上你想复制所有活动记录,以及任何没有具有相同 Emplid 的活动记录的撤回记录?
对,就是这样
那么,为什么不这样做:
XSLT 1.0
<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="act" match="Record[status='active']" use="Emplid" />
<xsl:template match="/Recordset">
<xsl:copy>
<xsl:copy-of select="Record[status='active']"/>
<xsl:copy-of select="Record[status='withdrawn'][not(key('act', Emplid))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果要保持原来的顺序,可以将两条xsl:copy-of
指令合二为一:
<xsl:copy-of select="Record[status='active'] | Record[status='withdrawn'][not(key('act', Emplid))]"/>
请注意使用键来解决交叉引用。
推荐阅读
- webassembly - 在我的 AssemblyScript 模块和我的 JS 之间共享内存的正确方法是什么?
- cucumber - 什么是更好的解决方案,在使用 Cucumber Java 时将测试数据保存在带有场景的功能文件或单独的文件(xml、json、yml)中?
- javascript - 如何使用纯javascript选择嵌套的ul元素
- javascript - caniuse和mozilla之间的Javascript Element scrollTo兼容性差异
- apache-spark - 如何在控制台上打印火花数据框名称
- java - 使用 Selenium Java 的 BrowserMobProxy
- algorithm - 如何避免 Firestore 中的 N + 1 个查询?
- javascript - 从 DevTools 控制台调用函数不会触发断点
- javascript - 一次又一次地调用异步函数,直到 Node JS 中的预期结果
- python - 如何将频谱图数据转换为张量(或多维 numpy 数组)?