Sean2148
Sean2148

Reputation: 365

XPath expression not finding matches in XSL

I'm trying to extract values from my RDF/XML file using an XSL document. The expression should extract the text content from the 'hasString' element, from every 'rdf:Description' element IF the description element has a 'type' attribute value that is either 'EnglishTerm' or 'GermanTerm'.

XPath Expression:

rdf:RDF/rdf:Description[@type = '#EnglishTerm' or @type = '#GermanTerm']/hasString[text()]

I have used an online XPath tester and for some reason only worked with one website, and did not work with others. I have tried to use this in an XSL document and it also did not work; the XPath did not match anything.

XSL

<?xml version="1.0" encoding="UTF-8"?>
<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#">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:for-each select="rdf:RDF/rdf:Description">
            <test>
                <xsl:value-of select = "rdf:Description[@type = '#EnglishTerm' or @type = '#GermanTerm']/hasString[text()]"/>
            </test> 
        </xsl:for-each>
    </xsl:template>     
</xsl:stylesheet>

XML Snippet

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
   <rdf:Description rdf:ID="T000001" rdf:type="#EnglishTerm">
      <hasConceptPreferredTermYN>Y</hasConceptPreferredTermYN>
      <isPermutedTermYN>N</isPermutedTermYN>
      <hasLexicalTag>LAB</hasLexicalTag>
      <hasRecordPreferredTermYN>N</hasRecordPreferredTermYN>
      <hasString rdf:parseType="Literal">A-23187</hasString>
      <hasDateCreated>1990.03.08</hasDateCreated>
      <hasAbbreviation/>
      <hasSortVersion/>
      <hasEntryVersion/>
      <hasThesaurusID rdf:parseType="Literal">NLM (1991)</hasThesaurusID>
      <hasTermNote/>
   </rdf:Description>
</rdf:RDF>

I'd like to know what I can do to fix my XPath expression so that it works with my XSL document.

Upvotes: 0

Views: 150

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

Within the for-each the context node is an rdf:Description element, your sample doesn't have them nested so trying xsl:value-of select = "rdf:Description inside is never going to select anything as it tries to select an rdf:Description child of the rdf:Description. Use . or self::node() or self::rdf:Description.

Upvotes: 0

Related Questions