Reputation: 831
I have some HTML something like this:
<tr class="odd">
<td class="player large-link">
<a name="some name"/> <!-- I want to return this but two times. -->
</td>
<td class="bookings">
<span/>
<span/>
</td>
</tr>
Is possible to return <a name="some name"/>
element two times based on the two <span/>
?
and sorry about my english.
Upvotes: 1
Views: 978
Reputation: 243599
Is possible to return
<a name="some name"/>
element two times based on the two<span/>
?
Not with a single XPath 1.0 expression -- The nodes selected by an XPath 1.0 expression form a so called "node-set" -- and this is a true set -- it is deduplicated.
In Xpath 2.0 one can construct a sequence of nodes and a sequence can contain the same node more than once:
/*/*/a[@name='some name']/*[1]/.., /*/*/a[@name='some name']/*[2]/..
The above XPath 2.0 expression specifies a sequence containing the two wanted, identical elements.
Upvotes: 1
Reputation: 12729
XPath 1.0 uses the concept of node sets, whereas XPath 2.0 uses sequences. Node sets are true sets, so you cant have the same node being a member of a node-set twice. You can however do the next best thing, which is to iterate of the set of identified span
nodes and in your higher level language, and then perform some action for each span node on the a
node.
For example, suppose you are using XPath within XSLT. Then this fragment of an XSLT 1.0 style-sheet will perform some action on the a
node as many times as you have span
nodes. This fragment assumes that on entry, the focus node is the parent of the tr
node.
<xsl:for-each select="tr/td[@class="bookings"]/span">
<xsl:for-each select="(../../td[@class='player large-link']/a[name='@some name'])[1]">
<!-- The `a` node is now the focus node. -->
<!-- Insert desired action here on the focus node. -->
</xsl:for-each>
</xsl:for-each>
Upvotes: 1
Reputation: 16927
That is impossible in XPath 1, but in XPath 2 you can use for..in..return:
for $s in tr/td[@class="bookings"]/span return $s/../preceding-sibling::td/a
(assuming the html parser has inserted closing td, span tags, so the td becomes a preceding element, not a parent)
Upvotes: 1