I have the next xpath:
//span[text()="Bingo"]/parent::div/parent::div/parent::td/preceding-sibling::td[1]
and it returns two elements.
How I choose one of them (first or second)?
I try:
//span[text()="Bingo"]/parent::div/parent::div/parent::td/preceding-sibling::td[1][1]
But it still gives me two elements.
Use parentheses over the whole expression, then an index:
(//span[text()="Bingo"]/parent::div/parent::div/parent::td/preceding-sibling::td[1])[1]
Related
I am dealing with an XML vocabulary that has "default values": i.e., if a node does not have a certain subnode, I'd like to find the nearest enclosing node that has that subnode, and use its string value as the value of the original node's subnode.
E.g., if I have a tree
Super_node
sub_node: XXX
...
context_node
/* does not have a child with name sub_node */
and no intervening nodes between Super_node and context_node have a child sub_node, I want an expression that evaluates to XXX.
I was thinking that the following query should work, but I always get a node list:
string(ancestor-or-self::*/sub_node[1]/text())
My thinking is that ancestor-or-self::* returns, in reverse document order, the list of context_node, parent_of_context_node, ..., Super_node. I apply the sub_node test to that, and get the list of sub_nodes in that list, again, hopefully, in reverse document order.
I then apply the predicate [1], which should return the first element of that list. However, this predicate seems to be not effective: in my implementation (which I think is based on libxml2), I still receive a list.
What does work, I found after poking around on Stack Exchange a bit, is
string((ancestor-or-self::*/sub_node)[last()]/text())
Why is the predicate [1] above not effective?
The expression
ancestor-or-self::*/sub_node[1]
means
ancestor-or-self::*/(child::sub_node[1])
which selects the first sub_node element child of every ancestor element.
I suspect you were thinking of
(ancestor-or-self::*/sub_node)[1]
which selects all the sub_node children of all the ancestor elements, sorts them into document order, and then returns the first node in this list.
Predicates like [1] bind more strongly than "/".
Let's say,
i=2
#driver.find_elements("...").get(i).click
How to define similar method in ruby; any tweak on the same?
Use this code to click that link
get method is not available for array, you can take first element by first and last element by last and if you want any other elements you could use [i]
#driver.find_elements("...")[i].click
In XPath you can use *[position()=1 or position()=last()] to get both the first and last matching node. However, if you want either just the first or last node you can use *[1] or *[last()] respectively. Trying to use something like *[1 or last()] selects all nodes. Is there a more concise way of joining the conditions?
Short answer: No. There is no more concise way than [position()=1 or position()=last()] that make sense for this purpose.
Regarding this predicate that you tried [1 or last()] :
number 0 translated to boolean False and the rest translated to True.
last() returns position index of the last element in context
Given above rules, this kind of predicate expressions [1 or last()] always translated to [True or True] which evaluates to True, that's why you get all nodes using this predicate.
Enumerable.Range(1, 999).Select((n,i) =>{ return n*i;})
what does the "i" get in every time?
Enumerable.Range(1, 999).Select((n,i,j) =>{ return n*i*j;})
why cant I add "j"?
The Select overload accepting a lambda with two parameters will take the first parameter from the sequence, and the second is the index of the element.
In your example i will always be n-1, so there is not much use of the second parameter. When working with non-trivial sequences or sequences of non-numeric types it can sometimes be an advantage to have the order number of the element available in the select expression.
There is no three parameter version. That's why (n,i,j) doesn't work.
The first argument to the Select() extension method on IEnumerable has two forms. One takes one argument (the current element of the enumeration) and the second two arguments (the current element and the index). There is no version that takes three arguments. See http://msdn.microsoft.com/en-us/library/bb548891.aspx for more information.
The i is the name you've used for the index of the element you are currently projecting. You can't specify a third parameter because there is no overload of Select which defines a delegate that takes three parameters.
You can either build new elements on the basis of value (Select(n)) or on the basis of value and index (Select(n, i)). Select() with three parameters is not defined.
Compare: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select
My rule requires me to apply them only to methods without 'get' as part of their name. In another words, my rules need to apply to only non-getter methods in the class. I know to get a hold of all the non-getter methods, I can use
//MethodDeclarator[not(contains(#Image,'get'))]
However, I don't know the syntax about where I insert my logic for the rules. Is it like
//MethodDeclarator[
not(contains(#Image,'get'))
'Some Rule Statements'
]
I saw the use of . in the beginning of statement inside [] in some example code. what are they used for?
In my particular case, I need to combine following pieces together but so far I am unable to accomplish it yet.
Piece 1:
//PrimaryExpression[not(PrimarySuffix/Arguments)]
Piece 2:
//MethodDeclarator[not(contains(#Image,'get'))]
Piece 3:
//PrimaryExpression[PrimaryPrefix/#Label='this']
You need to have at least some basic knowledge/understanding of XPath.
I saw the use of . in the beginning of statement inside [] in some
example code. what are they used for?
[] is called predicate. It must contain a boolean expression. It must immediately follow a node-test. This specifies an additional condition for a node that satisfies the node-test to be selected.
For example:
/*/num
selects all elements named num that are children of the top element of the XML document.
However, if we want to select only such num elements, whose value is an odd integer, we add this additional condition inside a predicate:
/*/num[. mod 2 = 1]
Now this last expression selects all elements named num that are children of the top element of the XML document and whose string value represents an odd integer.
. denotes the context node -- this is the node that has been selected so-far (or the starting node off which the complete XPath expression is evaluated).
In my particular case, I need to combine following pieces together ...
You forgot to say in what way / how the three expressions should be combined. In XPath some of the frequently used "combinators" are the operators and, or, and the function not().
For example, if you want to select elements that are selected by all three provided XPath expressions, you can use the and operator:
//PrimaryExpression
[not(PrimarySuffix/Arguments)
and
PrimaryPrefix/#Label='this'
]