xslt - 通过xpath获取元素的前一个兄弟

时间:2021-11-23 20:35:27

Im trying to get the values of the element skos:prefLabel which has a sibling skos:closeMatch. However, I'm seeing that there are other skos:prefLabel being preceding-siblings of skos:closeMatch. I'm currently working on the following:

我试图获取元素skos的值:prefLabel有一个兄弟skos:closeMatch。但是,我看到还有其他skos:prefLabel是skos的前兄弟:closeMatch。我目前正在开展以下工作:

<?xml version="1.0" encoding="utf-8" ?>
 <root>
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:skosxl="http://www.w3.org/2008/05/skos-xl#"
     ....(namespaces deleted for brevity)
     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="fa">آبیس ماریزی‌ای</skos:prefLabel>
   <skos:prefLabel xml:lang="en">Abies mariesii</skos:prefLabel>
    ....
  </skos:Concept>

  <skos:Concept>
    ....
  </skos:Concept>

  <skos:narrower rdf:resource="http://aims.fao.org/aos/agrovoc/c_1322232213779"/>
  <skos:narrower rdf:resource="http://aims.fao.org/aos/agrovoc/c_19"/>
  <skos:prefLabel xml:lang="ar">شوح</skos:prefLabel>
  <skos:prefLabel xml:lang="fa">آبیس</skos:prefLabel>
  <skos:prefLabel xml:lang="ko">전나무속</skos:prefLabel>
  <skos:prefLabel xml:lang="ja">モミ属</skos:prefLabel>
  <skos:prefLabel xml:lang="tr">Abies</skos:prefLabel>
     ....
  <skos:prefLabel xml:lang="es">Abies</skos:prefLabel>
  <skos:prefLabel xml:lang="en">Abies</skos:prefLabel>
     ....
  <skos:prefLabel xml:lang="hi">पलूदर</skos:prefLabel>
  <skos:prefLabel xml:lang="zh">冷杉属</skos:prefLabel>
  <skos:closeMatch>
   <rdf:Description rdf:about="http://d-nb.info/gnd/4184405-1">
    <skos:closeMatch rdf:resource="http://aims.fao.org/aos/agrovoc/c_10"/>
   </rdf:Description>
  </skos:closeMatch>
    ....
  </rdf:RDF>

The whole XSLT is the following:

整个XSLT如下:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    ....(namespaces deleted for brevity)
 xmlns:ns1="http://art.uniroma2.it/ontologies/vocbench#"
 xmlns:void="http://rdfs.org/ns/void#">
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

<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:for-each>
</xsl:template>

 <xsl:template match="skos:Concept/skos:prefLabel">
  <xsl:for-each select="//skos:closeMatch/preceding-sibling::skos:prefLabel" />
  <xsl:text>=306  \\$aclosematch$b</xsl:text>
  <xsl:value-of select="." />   
  <xsl:text>&#13;&#10;</xsl:text>
 </xsl:template> 
.....
</xsl:stylesheet>

So I should be getting the following:.

所以我应该得到以下内容:

=306  //$aشوح
=306  //$aآبیس
=306  //$a전나무속
 ....
=306  //$aAbies

With the xslt above, I am instead getting all skos:prefLabel, that's why I mentioned in the opening line of my question that there are many skos:prefLabel aside from the one nearest skos:closeMatch. I also tried putting in value of select=preceding-sibling::skos:prefLabel and preceding-sibling::skos:prefLabel1 and various combination, either I'm getting the same set of so many skos:prefLabels or blanks. I hope you can help me with my question. TIA and cheers!

使用上面的xslt,我得到所有skos:prefLabel,这就是为什么我在我的问题的开头行中提到有很多skos:prefLabel除了最近的skos:closeMatch。我也尝试将select = preceding-sibling :: skos:prefLabel和preceding-sibling :: skos:prefLabel1和各种组合的值放入其中,要么我得到相同的一组skos:prefLabels或blanks。我希望你能帮助我解决我的问题。 TIA和欢呼!

Note: the whole file is available here

注意:整个文件在这里可用

1 个解决方案

#1


0  

changing select="//skos:closeMatch/preceding-sibling::skos:prefLabel" to select="//skos:closeMatch/preceding-sibling::skos:prefLabel[1]" should do what you want I hope..

更改select =“// skos:closeMatch / preceding-sibling :: skos:prefLabel”来选择=“// skos:closeMatch / preceding-sibling :: skos:prefLabel [1]”应该做你想要的我希望..

#1


0  

changing select="//skos:closeMatch/preceding-sibling::skos:prefLabel" to select="//skos:closeMatch/preceding-sibling::skos:prefLabel[1]" should do what you want I hope..

更改select =“// skos:closeMatch / preceding-sibling :: skos:prefLabel”来选择=“// skos:closeMatch / preceding-sibling :: skos:prefLabel [1]”应该做你想要的我希望..