XSLT1.0:删除与xsl:key组合的重复项

时间:2013-11-07 15:51:52

标签: xml xslt xpath

我有以下XML:

<research>
  <research.record>
    <research.record_number>1</research.record_number>
    <research.type>
      <value lang="en-US">some research type</value>
    </research.type>
    <research.type>
      <value lang="en-US">some other type of research</value>
    </research.type>
    <project.record>
      <priref>101</priref>
      <project.type>
        <value lang="en-US">some type of project</value>
      </project.type>
    </project.record>
  </research.record>
</research>
<research>
  <research.record>
    <research.record_number>2</research.record_number>
    <research.type>
      <value lang="en-US">some other type of research</value>
    </research.type>
    <research.type>
      <value lang="en-US">a third type of research</value>
    </research.type>
    <project.record>
      <priref>101</priref>
      <project.type>
        <value lang="en-US">some type of project</value>
      </project.type>
    </project.record>
  </research.record>
</research>
<research>
  <research.record>
    <research.record_number>3</research.record_number>
    <research.type>
      <value lang="en-US">some other type of research</value>
    </research.type>
    <research.type>
      <value lang="en-US">a fourth type</value>
    </research.type>
    <project.record>
      <priref>201</priref>
      <project.type>
        <value lang="en-US">some other type of project</value>
      </project.type>
    </project.record>
  </research.record>
</research>
<research>
   ... etc ...

使用XSLT 1.0,我使用xsl:key将此XML转换为唯一项目记录列表。

到目前为止,非常好......

问题是:我还希望为每个独特的项目记录显示独特的研究类型。

我的简化样式表显示了重复的research.types(但不是唯一的)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:output method="html" indent="yes"/>

  <xsl:key name="uniqueProject" match="project.record" use="priref"/>

  <xsl:template match="record">
    <div class="table-row">
    <xsl:apply-templates select="//project.record[generate-id() = generate-id(key('uniqueProject', priref)[1])]">
        <xsl:sort select="startdate"/>
    </xsl:apply-templates>
    </div>
   </xsl:template>

   <xsl:template match="project.record">
     <div class="project-container">
       <xsl:text>project.record </xsl:text>
       <xsl:value-of select="priref"/>
       <xsl:text>: </xsl:text>
       <xsl:for-each select="//research/research.record/research.type[../project.record/priref = current()/priref]"> 
         <xsl:sort select="value[@lang='en-US']" data-type="text" />         
           <xsl:if test="value[@lang='en-US'][not(.=preceding::research/research.record/research.type/value[@lang='en-US'][../../project.record/priref = current()/priref])]">
             <xsl:value-of select="value[@lang='en-US']"/>
           </xsl:if>
           <xsl:if test="position()!=last()">
             <xsl:text>, </xsl:text>
           </xsl:if>
         </xsl:for-each>
       </div>
       <br/>
       <br/>
    </xsl:template>

</xsl:stylesheet>

我想要的输出是:

project.record 101: some research type, some other type of research, a third type of    research

project.record 201: some other type of research, a fourth type

希望有人可以帮助我使用正确的XSLT / XPATH。 (只能使用XSLT1.0)

1 个答案:

答案 0 :(得分:3)

我会通过使用第二个键来实现此目的,该键将research.type值组合为其值及其关联的priref。

<xsl:key name="resTypeKey" match="research.type/value[@lang='en-US']"
    use="concat(., '+++', ../../project.record/priref)" />

然后使用与桌面行相同的Muenchian技巧:

<xsl:template match="project.record">
  <div class="project-container">
    <xsl:text>project.record </xsl:text>
    <xsl:value-of select="priref"/>
    <xsl:text>: </xsl:text>
    <xsl:apply-templates select="
      key('uniqueProject', priref)/../research.type
        /value[@lang='en-US'][
          generate-id() = generate-id(
           key('resTypeKey', concat(., '+++', current()/priref))[1])]">
      <xsl:sort select="." />
    </xsl:apply-templates>
  </div>
  <br/>
  <br/>
</xsl:template>

<xsl:template match="research.type/value">
  <xsl:if test="position() &gt; 1">, </xsl:if>
  <xsl:value-of select="."/>
</xsl:template>
相关问题