Search code examples
xmlxslttei

XSL: Display value if similar in two different nodes


I am a beginner in XSLT. My content comes from TEI file.

<!-- language: XSLT -->
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE stylesheet [
 <!ENTITY menu SYSTEM "corpus.xml"> 


]> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml"
xpath-default-namespace="http://www.tei-c.org/ns/1.0" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="html" encoding="utf-8" doctype-system="about:legacy-compat"/>
<!-- delete extra blank lines -->
<xsl:strip-space elements="*"/>

<xsl:template match="/">
 <html>
  <head/>
  <body>
   <ul>
    <xsl:apply-templates select="descendant::taxonomy[2]/category[4]"/>
   </ul>
  </body>
 </html>
</xsl:template>

<xsl:template match="taxonomy[2]/category[4]">
<!-- several other 'xsl:for-each' within <ul> before the following <ul> --> 
<!-- each verb related to a sub-category ('category/category') in TEI -->
<ul>
 <xsl:for-each select="following-sibling::category/equiv[@n]">
  <li>
  <!-- @uri = @xml:id in TEI -->
    <xsl:variable name="href"><xsl:value-of select="@uri"/></xsl:variable>
    <a href="{$href}"> 
     <xsl:value-of select="./@*[namespace-uri()='http://www.w3.org/XML/1998/namespace' and local-name()='id']"/></a>:
   <!-- ### Problem starts here: find same value in element 'w' and element 'equiv' -->                            
   <xsl:for-each select="//w[@type='verb']">
   <!-- @xml:id in TEI -->
     <xsl:variable name="href"><xsl:value-of select="@xml:id"/> </xsl:variable>
     <a href="{$href}"> 
        <xsl:value-of select="./@*[namespace-uri()='http://www.w3.org/XML/1998/namespace' and local-name()='id']" /></a>
      <!-- look to first value of @ana of element 'w' = value of @xml:id of element 'equiv'  -->
         <xsl:if test="//w[@type='verb' and @ana[1]] = preceding::category/equiv/@*[namespace-uri()='http://www.w3.org/XML/1998/namespace' and local-name()='id']">
          <xsl:value-of select="//w[@type='verb'and @ana[1]]"/></xsl:if>
   </xsl:for-each>
  </li>
 </xsl:for-each>
</ul>
</xsl:stylesheet>

A section of my TEI-XML file:

<!-- language TEI-XML -->
<!-- XPATH: /teiCorpus/teiHeader[1]/encodingDesc[1]/classDecl[1]/taxonomy[2]/category[4]/category[9] -->
<category n="9" xml:id="verb.motion" ana="#verb.category #action">
 <catDesc>taxonomy: motion verbs</catDesc>
 <category n="1" xml:id="meeting" ana="#verb.motion"> 
  <catDesc>subcategory of motion's verb as a concept: meeting  
  </catDesc>
  <category ana="#transcription" xml:lang="uga">
   <equiv n="1" xml:id="qry"/>
  </category>
 </category>

<!-- section taht gave me trouble to find data in XSLT -->
<!-- XPATH /teiCorpus/text[1]/body[1]/div1[2]/div2[2]/div3[2]/div4[2]/lg[1]/l[1]/w[2] -->
<w type="verb" ana="#qry #yQTL" xml:id="ktu1-3_ii_l4b-5a_tqry">

For example: If category/equiv[@xml:id] and w[@type='verb' and @ana[1]] = 'qry' (always first value of @ana), display "href" of @xml:id of element 'w'.

I have no problem with first select="following-sibling::category/equiv[@n]". I have what I want. Unfortunately for select="//w[@type='verb']", what I have tried doesn't work: instead of a similar "href" of an @xml:id, I have ALL "href" and first value of @ana.

Current result:

qry: ktu1-3_ii_l3b-4a_klʾatklʾat tqry tmtḫṣ tḫtṣ tmḫṣ tṣmt ʿtkt šnst tġll tgrš tmġyn tštql šbʿt tṯʿr ṯʿr tmtḫṣn tʿn tḫtṣb tḥdy t[d]ġdd ktu1-3_ii_l4b-5a_tqryklʾat tqry tmtḫṣ tḫtṣ tmḫṣ tṣmt ʿtkt šnst tġll tgrš tmġyn tštql šbʿt tṯʿr ṯʿr tmtḫṣn tʿn tḫtṣb tḥdy t[d]ġdd

Instead of:

qry: ktu1-3_ii_l4b-5a_tqry 'tqry', ktu1-4_ii_l10_qryt 'qryt'.

Maybe the problem comes from <xsl:if> which is not the good choice?

Thanks for your kind help.


Solution

  • I don't quite follow your question, but if you are trying to get a w node with a specific value in the ana attribute, you might benefit from a key.

    <xsl:key name="w" match="w" use="tokenize(@ana, ' ')" />
    

    Then, if you are positioned on the equiv element, you would get the matching w node like so

    <xsl:for-each select="key('w', concat('#', @xml:id))">