xslt 删除重复元素

编程入门 行业动态 更新时间:2024-10-11 17:26:52
本文介绍了xslt 删除重复元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从该文件中删除具有相同 href 属性的元素:

I am trying to remove elements with the same href attribute form this file:

<?xml version="1.0" encoding="UTF-8"?> <images> <item id="d1e152_1" href="Pic/104764.jpg" media-type="image/jpeg"/> <item id="d1e163_2" href="Pic/104764.jpg" media-type="image/jpeg"/> <item id="d1e174_3" href="Pic/104764.jpg" media-type="image/jpeg"/> <item id="d1e218_4" href="Pic/104763.jpg" media-type="image/jpeg"/> <item id="d1e349_5" href="Pic/110001.tif" media-type="image/tif"/> <item id="d1e681_6" href="Pic/199201.tif" media-type="image/tif"/> <item id="d1e688_7" href="Pic/124566.tif" media-type="image/tif"/> </images>

使用这个 xslt 2.0 样式表

using this xslt 2.0 stylesheet

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="www.w3/1999/XSL/Transform" xmlns:fo="www.w3/1999/XSL/Format" xmlns:xs="www.w3/2001/XMLSchema" xmlns:fn="www.w3/2005/xpath-functions" exclude-result-prefixes="fn xs fo"> <xsl:output method="xml"/> <xsl:strip-space elements="*"/> <xsl:template match="/images"> <new_images> <xsl:apply-templates/> </new_images> </xsl:template> <xsl:template match="item"> <xsl:for-each-group select="." group-by="@href"> <xsl:copy-of select="current-group()[1]"/> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>

这里的预期结果:

<?xml version="1.0" encoding="UTF-8"?> <new_images> <item id="d1e152_1" href="Pic/104764.jpg" media-type="image/jpeg"/> <item id="d1e218_4" href="Pic/104763.jpg" media-type="image/jpeg"/> <item id="d1e349_5" href="Pic/110001.tif" media-type="image/tif"/> <item id="d1e681_6" href="Pic/199201.tif" media-type="image/tif"/> <item id="d1e688_7" href="Pic/124566.tif" media-type="image/tif"/> </new_images>

为什么不起作用?我在生成的 xsl 文件中仍然有所有重复项.

Why doesnt it work? I still have all duplicates in the generated xsl file.

推荐答案

这个 XSLT 1.0 解决方案更短而不是更慢:

<xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:key name="kByHref" match="item" use="@href"/> <xsl:template match="/*"> <new_images> <xsl:copy-of select= "*[generate-id() = generate-id(key('kByHref', @href)[1])]"/> </new_images> </xsl:template> </xsl:stylesheet>

更多推荐

xslt 删除重复元素

本文发布于:2023-11-11 05:15:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577476.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   xslt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!