xslt exclude repeated element values

202 Views Asked by At

I'm working with xsl keys and I want to get exclude repeated element values, I have the following xml:

<?xml version="1.0" encoding="utf-8" ?>
 <root>
  <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
          .....
  xmlns:xml="http://www.w3.org/XML/1998/namespace">

  <skos:Concept rdf:about="http://aims.fao.org/aos/agrovoc/c_26321">
           .....
   <skos:prefLabel xml:lang="en">Abies mariesii</skos:prefLabel>
           .....
   <skos:broader rdf:resource="http://aims.fao.org/aos/agrovoc/c_10"/>
  </skos:Concept>

  <skos:Concept rdf:about="http://aims.fao.org/aos/agrovoc/c_33272">
           .....
   <skos:prefLabel xml:lang="en">Abies numidica</skos:prefLabel>
            .....
   <skos:broader rdf:resource="http://aims.fao.org/aos/agrovoc/c_10"/>
  </skos:Concept>

  <skos:Concept rdf:about="http://aims.fao.org/skosmos/agrovoc/en/page/c_10">
            .....
   <skos:narrower rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_26321"/>
             .....
  </skos:Concept>

 </rdf:RDF>

Please take note that the skos:broader rdf:resource="http://aims.fao.org/aos/agrovoc/c_10" which is a child of skos:Concept has been repeated many times in the document and the full file can be accessed here: 128.199.159.143/merged-file.xml.

I am using the following xslt which was based from xslt get element value based on attribute which is referenced in another node tree using keys to output my file into text:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
              .....
 xmlns:void="http://rdfs.org/ns/void#">
 <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
 <xsl:key name="concepts-by-about" match="//skos:Concept" use="@rdf:about" />

  <xsl:template match="root">
   <xsl:for-each select="rdf:RDF">
   <xsl:text>START HERE</xsl:text>
   <xsl:text>&#13;&#10;</xsl:text>
   <xsl:text>=LDR  00000nam  2200000Ia 4500</xsl:text>
   <xsl:text>&#13;&#10;</xsl:text>
             ....
    <xsl:apply-templates select="skos:Concept" />
    <xsl:text>&#13;&#10;</xsl:text>
              ....
    <xsl:apply-templates select="skos:Concept/skos:narrower" />
    <xsl:text>&#13;&#10;</xsl:text>
    <xsl:apply-templates select="skos:Concept/skos:broader" />
    <xsl:text>&#13;&#10;</xsl:text>
   </xsl:for-each>
  </xsl:template>

  <xsl:template match="skos:Concept/skos:broader[key('concepts-by-about', @rdf:resource)]">       
   <xsl:text>=301  \\$abroader$b</xsl:text>
   <xsl:value-of select="key('concepts-by-about', @rdf:resource)/skos:prefLabel[@xml:lang='en']" />
   <xsl:text>$c</xsl:text>
   <xsl:value-of select="./@rdf:resource" />
   <xsl:text>&#13;&#10;</xsl:text>   
  </xsl:template>           

  <xsl:template match="text()" />

  <xsl:template match="//skos:narrower[key('concepts-by-about', @rdf:resource)]">       
   <xsl:text>=302  \\$anarrower$b</xsl:text>
   <xsl:value-of select="key('concepts-by-about', @rdf:resource)/skos:prefLabel[@xml:lang='en']" />
   <xsl:text>$c</xsl:text>
   <xsl:value-of select="./@rdf:resource" />
   <xsl:text>&#13;&#10;</xsl:text>   
  </xsl:template>           

  <xsl:template match="text()" />

 </xsl:stylesheet>

With this xslt I'm getting numerous =301 \$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10. Sample file printed is below:

START HERE
=LDR  00000nam  2200000Ia 4500
       ....
=302  \\$anarrower$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10
       ....
=302  \\$anarrower$bAbies pinsapo$chttp://aims.fao.org/aos/agrovoc/c_34587
=302  \\$anarrower$bAbies alba$chttp://aims.fao.org/aos/agrovoc/c_11
       ....
=301  \\$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10

So my question is how do I exclude the repeated values or is there a way to output these repeated values in one line, like below:

=310  \\$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10

Thanks in advance and cheers!

1

There are 1 best solutions below

0
On

I can't comment, so I have to use the answer-form.

If you're looking for distinct values, you could try some of these suggestions: How to use XSLT to create distinct values