首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xslt删除重复元素

xslt删除重复元素
EN

Stack Overflow用户
提问于 2015-12-04 12:09:30
回答 2查看 2.9K关注 0票数 1

我试图从这个文件中删除具有相同href属性的元素:

代码语言:javascript
复制
<?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>

使用此XSLT2.0样式表

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/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>

预期结果如下:

代码语言:javascript
复制
<?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文件中仍然有所有的重复项。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-05 18:36:38

此XSLT1.0解决方案更短,而不是更慢:

代码语言:javascript
复制
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/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>
票数 1
EN

Stack Overflow用户

发布于 2015-12-04 13:02:08

我拿到了..。总是发生在我身上..。把每个组都放在/images的模板里.

更新的XSLT 2.0..。

代码语言:javascript
复制
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs fo">

  <xsl:output method="xml"/>

  <xsl:strip-space elements="*"/>

  <xsl:template match="/images">
    <new_images>
      <xsl:for-each-group select="item" group-by="@href">
        <xsl:copy-of select="current-group()[1]"/>
      </xsl:for-each-group>
    </new_images>
  </xsl:template>

</xsl:stylesheet>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34088088

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档