我是 XSLT 的新手,在修改 xml 时遇到问题。
输入 XML 是:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<dictionary>
<label>A</label>
<brand>top</brand>
<color>black</color>
</dictionary>
<dictionary>
<label>B</label>
<brand>lower</brand>
<color>brown</color>
</dictionary>
<dictionary>
<label>A</label>
<brand>lower</brand>
<color>yello</color>
</dictionary>
<dictionary>
<label>C</label>
<brand>middle</brand>
<color>orange</color>
</dictionary>
<dictionary>
<label>B</label>
<brand>top</brand>
<color>blue</color>
</dictionary>
<dictionary>
<label>D</label>
<brand>mid</brand>
<color>green</color>
</dictionary>
<dictionary>
<label>A</label>
<brand>mid</brand>
<color>yello</color>
</dictionary>
</xml>
它包含一些以 A 作为子元素之一的元素。我想删除包含重复项的字典节点。此外,虽然它删除了重复项,但它应该只删除也包含 != lower 的重复节点
预期的输出是:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<dictionary>
<label>B</label>
<brand>lower</brand>
<color>brown</color>
</dictionary>
<dictionary>
<label>A</label>
<brand>lower</brand>
<color>yello</color>
</dictionary>
<dictionary>
<label>C</label>
<brand>middle</brand>
<color>orange</color>
</dictionary>
<dictionary>
<label>D</label>
<brand>mid</brand>
<color>green</color>
</dictionary>
</xml>
在互联网上,我找到了一个 xsl 来删除基于元素值的重复节点。删除重复节点的 xsl 是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="LABEL" match="dictionary" use="label"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="dictionary[not(generate-id() = generate-id(key('LABEL', label)[1]))]" />
</xsl:stylesheet>