xslt get element value based on attribute which is referenced in another node tree

限于喜欢 提交于 2019-11-28 12:40:23

问题


I have the following xml file:

 <?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#"
   xmlns:skos="http://www.w3.org/2004/02/skos/core#"
   xmlns:dc="http://purl.org/dc/terms/"
   xmlns:ns0="http://art.uniroma2.it/ontologies/vocbench#"
   xmlns:void="http://rdfs.org/ns/void#">

  <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: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_1591">
   <skos:prefLabel xml:lang="ar">أشجار عيد الميلاد</skos:prefLabel>
        ....
   <skos:prefLabel xml:lang="en">christmas trees</skos:prefLabel>
     ....

  </skos:Concept>

     ....

  <skos:Concept>
   <ns0:isUsedAs rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_7776"/>
   <ns0:isUsedAs rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_1591"/>
  </skos:Concept>

 </rdf:RDF>    
 </root>

I want to get the value of <skos:prefLabel xml:lang="en"> which has a parent skos:Concept and this skos:Concept is referenced in another node tree ns0:isUsedAs. Thus, we get the value of 'christmas tree' for ns0:isUsedAs rdf:resource="http://aims.fao.org/skosmos/agrovoc/en/page/c_1591. And I want to output this as text, same as below:

 =305  \\$aisUsedAs$bchristmas tree

Please take note that skos:prefLabel is a child of skos:Concept. ns0:isUsedas is a also child of skos:Concept, but is in another node tree. I also already have the following preliminary xsl:templates:

 <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="rdf:Description/skos:narrowMatch" />
   <xsl:text>&#13;&#10;</xsl:text>
  <xsl:apply-templates select="rdf:Description/skos:exactMatch" />
   <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:altLabel" />
   <xsl:text>&#13;&#10;</xsl:text>
  <xsl:apply-templates select="skos:Concept/skos:prefLabel" />
   <xsl:text>&#13;&#10;</xsl:text>  
  </xsl:for-each>
 </xsl:template>

I hope you can help me with my problem. Thanks in advance!

further update:

Here is an xslt based from Dan's answer, but I'm still getting blanks:

 <xsl:transform
  ......
 >
<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="rdf:Description/skos:narrowMatch" />
   <xsl:text>&#13;&#10;</xsl:text>
   <xsl:apply-templates select="rdf:Description/skos:exactMatch" />
   <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:altLabel" />
   <xsl:text>&#13;&#10;</xsl:text>
   <xsl:apply-templates select="skos:Concept/skos:prefLabel" />
   <xsl:text>&#13;&#10;</xsl:text>  
  </xsl:for-each>
 </xsl:template>
 <xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8" indent="no" />
 <xsl:key name="concepts-by-about" match="//skos:Concept" use="@rdf:about" />

    <xsl:template match="//ns0:isUsedAs[key('concepts-by-about', @rdf:resource)]">        
=305 \\$aisUsedBy$b<xsl:value-of select="key('concepts-by-about', @rdf:resource)/skos:prefLabel[@xml:lang='en']" />        
    </xsl:template>

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

回答1:


This should get you started:

<?xml version="1.0"?>

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

  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:skos="http://www.w3.org/2004/02/skos/core#"
  xmlns:ns0="http://art.uniroma2.it/ontologies/vocbench#"
>

  <xsl:template match="/">
    <xsl:for-each select="//skos:Concept/ns0:isUsedAs/@rdf:resource">
      <xsl:variable name='resource' select="."/>
      <xsl:value-of select="//skos:Concept[@rdf:about=$resource]/skos:prefLabel[@xml:lang='en']"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>



回答2:


Using keys:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       xmlns:skosxl="http://www.w3.org/2008/05/skos-xl#"
       xmlns:skos="http://www.w3.org/2004/02/skos/core#"
       xmlns:dc="http://purl.org/dc/terms/"
       xmlns:ns0="http://art.uniroma2.it/ontologies/vocbench#"
       xmlns:void="http://rdfs.org/ns/void#">
        <xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8" indent="no" />
        <xsl:key name="concepts-by-about" match="//skos:Concept" use="@rdf:about" />

        <xsl:template match="//ns0:isUsedAs[key('concepts-by-about', @rdf:resource)]">        
    =305 \\$aisUsedBy$b<xsl:value-of select="key('concepts-by-about', @rdf:resource)/skos:prefLabel[@xml:lang='en']" />        
        </xsl:template>

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

The key gets you quick access to a Concept using its rdf:about attribute. This template will grab any ns0:isUsedAs that has a corresponding skos:Concept based on the sepcified attributes.



来源:https://stackoverflow.com/questions/31700890/xslt-get-element-value-based-on-attribute-which-is-referenced-in-another-node-tr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!