xslt xpath using element values in xpath query

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-17 04:57:05

问题


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:


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.



来源:https://stackoverflow.com/questions/32136173/xslt-xpath-using-element-values-in-xpath-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!