Search code examples
xmlxsltparent-childrdfskos

xsl to get value of attribute based on child node element


I have the following skosxl file:

<skos:Concept rdf:about="http://aims.fao.org/aos/agrovoc/c_0e0e555b">
   <ns0:hasStatus>Published</ns0:hasStatus>
   <skos:prefLabel xml:lang="es">baldíos</skos:prefLabel>
   <skos:prefLabel xml:lang="en">unclaimed lands</skos:prefLabel>
   <skos:prefLabel xml:lang="fr">terres vacantes</skos:prefLabel>
   <dc:created rdf:datatype="xsd:http://www.w3.org/2001/XMLSchema#dateTime">2015-03-09T14:50:41Z</dc:created>
   <skos:definition>
     <rdf:Description rdf:about="http://aims.fao.org/aos/agrovoc/def_9343e456">
        <rdf:value xml:lang="en">Unclaimed land is land for which there is no owner or claimant.</rdf:value>
     </rdf:Description>
   </skos:definition>
   .....

<rdf:RDF>

I wanted to get the value of rdf:about of skos:Concept which is bases on child node ns0:hasStatus, the following I can't seem to make it work:

<xsl:template match="root">
  <xsl:for-each select="rdf:RDF">
    <xsl:text>STARTHERE</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/skos:narrower" />
    <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">
  <xsl:if test="child::ns0:hasStatus">
    <xsl:text>=300  \\$a</xsl:text><xsl:value-of select="@rdf:about" />
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:if>
</xsl:template>

<xsl:template match="skos:Concept">
  <xsl:if test="skos:broader">
    <xsl:for-each select="skos:prefLabel" />
    <xsl:text>=301  \\$a</xsl:text><xsl:value-of select="skos:prefLabel[@xml:lang='en']" />
    <xsl:text>&#13;&#10;</xsl:text>
  </xsl:if>
</xsl:template>

Thanks in advance!


Solution

  • You can use XPath skos:Concept[ns0:hasStatus] to match <skos:Concept> element that has child <ns0:hasStatus> :

    <xsl:template match="skos:Concept[ns0:hasStatus]">
      <xsl:text>=300  \\$a</xsl:text><xsl:value-of select="@rdf:about" />
      <xsl:text>&#13;&#10;</xsl:text>
    </xsl:template>