0

Is it possible to use the element values in xpath? I have the following xml:

<root>
 <html>
  <table class=" table search-results-property-table">  
                 ....
   <tr>
    <td>
     <span class="versal">HAS TAXONOMIC LEVEL</span>
    </td>
    <td>
     <ul>
      <li>
       <a class="versal" href="../../../agrovoc/en/page/c_11125">genus</a>
      </li>
     </ul>
    </td>
   </tr>
   <tr>
    <td>
     <span class="versal">IS USED AS</span>
    </td>
    <td>
     <ul>
      <li>
       <a class="versal" href="../../../agrovoc/en/page/c_1591">christmas trees</a>
      </li>
      <li>
       <a class="versal" href="../../../agrovoc/en/page/c_7776">timber trees</a>
      </li>
     </ul>
    </td>
   </tr>
    ....
  <table>
 </html>
 <html>
   [second data set...]
    ...
<root>

I want to have the element values HAS TAXONOMIC LEVEL and IS USED AS be used in xpath. Next is to output their values (e.g. HAS TAXONOMIC LEVEL) and then get the values of descendants of sibling td: in /ul/li/a -> genus under HAS TAXONOMIC LEVEL and christmas trees and timber trees under IS USED AS. So I get the following:

START HERE
=LDR  00000nam  2200000Ia 4500
=305  \\$aHAS TAXONOMIC LEVEL$bgenus
=305  \\$aIS USED AS$bchrismas trees
=305  \\$aIS USED AS$btimber trees

START HERE
=LDR  00000nam  2200000Ia 4500
  (second data set and so on..)

Please take note that I have more than one document in this file with xml format like below:

<root>
 <html>
   [DATA SET 1]
         ....
 </html>
      <html>
   [DATA SET 2]
         ....
 </html>
       .....
</root>

Thanks and cheers!

1 Answer 1

1

Try something like:

<xsl:template match="/root">
    <xsl:for-each select="html/table/tr[td/span='HAS TAXONOMIC LEVEL' or td/span='IS USED AS']">
        <xsl:variable name="span" select="td/span" />
        <xsl:for-each select="td/ul/li/a">
            <xsl:text>$new-line </xsl:text>
            <xsl:value-of select="$span"/>
            <xsl:text> $separator </xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:for-each> 
</xsl:template>

and then get the values below them in /ul/li/a

You are mistaken about this: the values are not "below" them; they are descendants of a sibling td.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This works, now I have to integrate it in my whole xslt. I have a follow-up question though, how about blindly getting all element values of span class="versal" and use the values as placeholders and get the values of the descendants of sibling td. I was actually trying to answer my question through keys, but your answer resolves my question. Thanks and cheers!
@schnydszch "how about blindly getting all element values of span class="versal" and use the values as placeholders and get the values of the descendants of sibling td." I am not sure what you mean by that. Why don't you post this a separate question and explain in more detail?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.