我一直在研究以下xml:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
.....
xmlns:dc="http://purl.org/dc/terms/">
<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/aos/agrovoc/c_5886">
.....
<skos:prefLabel xml:lang="en">Pinaceae</skos:prefLabel>
.....
<skos:narrower rdf:resource="http://aims.fao.org/aos/agrovoc/c_10"/>
</skos:Concept>
<skos:Concept>
<skos:narrower rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_1322232213779"/>
<skos:narrower rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_19"/>
.....
<skos:prefLabel xml:lang="en">Abies</skos:prefLabel>
.....
<skos:closeMatch>
.....
</rdf:RDF>
整个 xml 可以在这里访问:http: //128.199.159.143/merged-file.xml
我在这里有以下 XSLT(基于xslt get element value based on attribute which is referenced in another node tree):
<?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> </xsl:text>
<xsl:text>=LDR 00000nam 2200000Ia 4500</xsl:text>
<xsl:text> </xsl:text>
<xsl:apply-templates select="skos:Concept/skos:broader" />
<xsl:text> </xsl:text>
<xsl:apply-templates select="skos:Concept/skos:narrower" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="//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> </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> </xsl:text>
</xsl:template>
<xsl:template match="text()" />
<xsl:template match="skos:Concept/skos:narrower/skos:Concept/skos:prefLabel[@xml:lang='en']">
<xsl:text>=302 \\$anarrower$b</xsl:text><xsl:value-of select="." />
<xsl:text>$c</xsl:text>
<xsl:value-of select="../@rdf:about" />
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
有了这个 xslt,我得到以下信息:
START HERE
=LDR 00000nam 2200000Ia 4500
=301 \\$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10
=301 \\$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10
(more =301 \\$abroader$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10) ...
=301 \\$abroader$bPinaceae$chttp://aims.fao.org/aos/agrovoc/c_5886
这对我来说很好,因为这是我想要的 xml,基于http://aims.fao.org/skosmos/agrovoc/en/page/c_10松科是一个更广泛的概念。但是,“Abies”重复了几次,那么如何删除这些重复项并将其打印为另一个值:
=400 \\$apreferredterm$bAbies$chttp://aims.fao.org/aos/agrovoc/c_10