0

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>
schnydszch
  • 435
  • 5
  • 19

2 Answers2

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>
Kenney
  • 9,003
  • 15
  • 21
  • I see that there is [2] in usedAs, so this mean it will get the value of the second variable. How about for every variable? Same as xsl:for-each? Thanks! – schnydszch Jul 29 '15 at 16:11
  • Yes exactly. Updated the answer. – Kenney Jul 29 '15 at 16:26
  • Hi Kenney, I'm trying out your answer but I'm getting blanks. I'm trying it out with this xml: http://128.199.159.143/merged-file.xml – schnydszch Aug 05 '15 at 11:21
  • That is because ns0 was `"http://art.uniroma2.it/ontologies/vocbench#"` in your original file, and is `"http://aims.fao.org/aos/agrontology#"` in the merged-file.xml. – Kenney Aug 05 '15 at 15:45
  • Thanks Kenney, I've been banging my head on this, it's just the namespace. :P I didn't see that. Oh well, that should be basic. And so all other things blank probably the namespace. And now, to integrate this to the whole template I am working on. Cheers! – schnydszch Aug 05 '15 at 19:01
  • Hi Kenney! I'm trying your answer again. But your template match is match is in root. How do I adjust it in my whole template that has the following: START HERE =LDR 00000nam 2200000Ia 4500 – schnydszch Aug 11 '15 at 09:59
0

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.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • Hi! I'm trying out your answer, however I don't know what template should I put in the beginning of my xslt, I edited my question to indicate these templates. Do I put I'm still figuring out what to put in here. Thanks and cheers! – schnydszch Jul 30 '15 at 07:42
  • Philip Wendler, deleted it already. I just put the "answer-cum-question" in the updates. The thing is when I add a comment with codes, it is not printed correctly in the screen. Thus, I posted in answer. I just edited my question with an update on Dan's answer instead and delete(d) this answer. Thanks! – schnydszch Jul 30 '15 at 09:51
  • You've changed the key. The way you have it in you rposted code, it won't ever match any nodes. Change it to what I have for the key and it should work. You also probably want a template to absorb any remaining unwanted text, as in the last template from my example. – Dan Field Jul 30 '15 at 17:48
  • Yeah i've also tried it in a new template but just added and templates at the beginning. Either i was getting blank or getting all element value of skos:prefLabel with attribute 'en'. And also just copy pasting the whole code gives me only . I tried to incorporate it also with my current template which you can see in the update. I'll try it again tomorrow as it's already midnight here. Thanks! – schnydszch Jul 30 '15 at 21:37
  • Hi Dan! Please see update on my question. I'm still getting blanks, even just copy pasting the code. I added templates I only get START HERE =LDR 00000nam 2200000Ia 4500 – schnydszch Jul 31 '15 at 07:41
  • Hello again Dan! I feel that your answer is correct however I don't still get what I wanted out of the data. The data can be found here http://128.199.159.143/merged-file.xml Thanks in advance and have a nice day! – schnydszch Aug 04 '15 at 08:42
  • Hi Dan! Just like with Kenney's answer, your answer is correct. There was a problem in the namespaces. Thanks again and cheers! :) – schnydszch Aug 06 '15 at 09:28