问题
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> </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