2017-03-01 60 views
1

这不是完全删除我在此论坛中找到的重复项的主题。XSLT - 从映射结果中删除重复项

我有一个键/值映射,我想从映射的最终结果中删除重复项。

源文档:

<article> 
    <subject code="T020-060"/> 
    <subject code="T020-010"/> 
    <subject code="T090"/> 
</article> 

映射:

<xsl:variable name="topicalMap"> 
    <topic MapCode="T020-060">Value 1</topic> 
    <topic MapCode="T020-010">Value 1</topic> 
    <topic MapCode="T090">Value 3</topic> 
</xsl:variable> 

所需的结果:

<article> 
    <topic>Value 1</topic> 
    <topic>Value 3</topic> 
</article> 

XSLT我与(注工作,它有一个测试标签和代码确保映射工作):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 

<xsl:output method="xml" encoding="utf8" indent="yes" exclude-result-prefixes="#all"/> 

<xsl:template match="article"> 
    <article> 
     <xsl:for-each-group select="subject" group-by="$topicalMap/topic[@MapCode = @code]"> 
      <test-group> 
       <code>Current code: <xsl:value-of select="@code"/></code> 
       <topic>Current keyword: <xsl:value-of 
         select="$topicalMap/topic[@MapCode = @code]"/></topic> 
      </test-group> 
     </xsl:for-each-group> 
     <simple-mapping><xsl:apply-templates/></simple-mapping> 
    </article> 
</xsl:template> 


<!-- Simple Mapping Topics --> 
<xsl:template match="subject"> 
    <xsl:variable name="ArticleCode" select="@code"/> 
    <topic> 
     <xsl:value-of select="$topicalMap/topic[@MapCode = $ArticleCode]"/> 
    </topic> 
</xsl:template> 

<!-- Keyword Map --> 
<xsl:variable name="topicalMap"> 
    <topic MapCode="T020-060">Value 1</topic> 
    <topic MapCode="T020-010">Value 1</topic> 
    <topic MapCode="T090">Value 3</topic> 
</xsl:variable> 

</xsl:stylesheet> 

通过这种方式做组不会产生任何东西。如果我复制源文档中的主题,并在应用映射之前执行group-by =“@ code”,它可以删除。但我想删除结果重复的值不重复的键。

简单映射的东西只是为了显示工作代码。

回答

2

使用

<xsl:for-each-group select="subject" group-by="$topicalMap/topic[@MapCode = current()/@code]"> 
     <topic> 
      <xsl:value-of select="current-grouping-key()"/> 
     </topic> 
    </xsl:for-each-group> 

或更好,但

<xsl:key name="map" match="topic" use="@MapCode"/> 


<xsl:template match="article"> 
    <article> 
     <xsl:for-each-group select="subject" group-by="key('map', @code, $topicalMap)"> 
      <topic> 
       <xsl:value-of select="current-grouping-key()"/> 
      </topic> 
     </xsl:for-each-group> 
    </article> 
</xsl:template> 
+0

什么是使用密钥(),使得它更好的效益? – LadyCygnus

+2

给定''处理器通过MapCode属性值对'topic'元素进行索引,然后通过重复访问找到匹配直接使用索引而不是搜索所有元素。 –