0
votes

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.

1
Posting snippets that don't declare the namespaces they use are meaningless, so please add a minimal but complete snippet to allow us to reproduce the problem. - Martin Honnen
I have added the namespaces, however I don't see how that information is needed for my question. It should be safe to assume that the namespaces have been declared if they are being used in the XML no? - Sean2148
I would say that 90 % of the questions of XSLT/XPath not selecting or matching something here on StackOverflow are due to the lack of use or wrong use or simply typos in namespace uses so assuming correct use of them doesn't help. - Martin Honnen

1 Answers

0
votes

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.