I have an XPATH expression of the following sort that's expected to return a boolean value:
xs:boolean(expression1 or expression2 or expression3)
If expression1 returns true, would the other expressions be evaluated?
In any case could any one point me to examples of how complex logical expressions are written efficiently in XPATH?
BTW: I am running the XPATH on MarkLogic.
In XPath 1.0 it's defined that the expressions are evaluated in order, left to right, until one of them returns true.
But the presence of xs:boolean (which is redundant) in your expression suggests you are using XPath 2.0, and XPath 2.0 processors are allowed to evaluate the subexpressions in any order. This is to allow database-style optimization: one of the subexpressions might be much faster to execute (or more likely to return true) than the others, perhaps because of database indexes, so an optimizer will evaluate that one first. But any decent optimizer will stop evaluation after the first expression that evaluates to "true".
I can't tell you specifically what MarkLogic does.
For anyone else trying this, the "or" operator in XPath must be lower-case.
In light of Michael Kay's comments on optimization, I can't say for sure whether MarkLogic chooses expression to evaluate first or goes left to right, but you can see how a particular XPath is evaluated. In Query Console (usually localhost:8000/qconsole), type in an expression, click the Profile tab, and Run.
//foo[xs:boolean(1 = 1 or 2 = 3)]
The profile tab shows that "1 = 1" is evaluated and "2 = 3" is not.
Related
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.
I'm using soapUI 5 (non pro) and all i need is to validate(assert) a number is greater than zero in the expected result section. So this means
1) in XPath expression(Xpath match) I am declaring the below (I need to remove all text and only have numbers then check that number is greater than zero)
replace(//OUTBOUND_MESSAGE.MESSAGE_CONTENT, '[^0-9]','')
2) All i want to do in expected result is =!0 or number>0 so i attempted
${=!0} but doing that brings back a boolean T/F. I'm really lost :(
The expression ${=!0} is not working as you expect. In SOAPUI this kind of expressions ${=expression} are executed as groovy script so really SOAPUI is executing !0 which is result is true and this is the expected result. This is why SOAPUI throws replace..., expecting [true].
I think that it's better to change you XPath expression to evaluate directly if your expression is >0:
number(replace(//OUTBOUND_MESSAGE.MESSAGE_CONTENT, '[^0-9]',''))>0
And as expected result simply set true.
new to TCL and running into a short circuit issue it seems. Coming from vbscript, I'm able to perform this properly, but trying to convert to a TCL script I'm having issues with the short circuit side effect and have been trying to find the proper way of doing this.
In the following snippet, I want to execute "do something" only if BOTH sides are true, but because of short circuiting, it will only evaluate the second argument if the first fails to determine the value of the expression.
if {$basehour != 23 && $hours != 0} {
do something
}
Maybe I'm not searching for the right things, but so far I've been unable to find the solution. Any tips would be appreciated.
The && operator always does short-circuiting in Tcl (as it does in C and Java and a number of other languages too). If you want the other version and can guarantee that both sub-expressions yield booleans (e.g., they come from equality tests such as you're doing) then you can use the & operator instead, which does bit-wise AND and will do what you want when working on bools. If you're doing this, it's wise to put parentheses around the sub-expressions for clarity; while everyone remember the precedence of == with respect to &&, the order w.r.t. & is often forgotten. (The parentheses are free in terms of execution cost.)
if {($basehour != 23) & ($hours != 0)} {
do something
}
However, it's usually not necessary to do this. If you're wanting an AND that you're feeding into a boolean test (e.g., the if command's expression) then there's no reason to not short-circuit, as in your original code; if the first clause gives false, the second one won't change what value the overall expression produces.
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'
]
I have the following sample XML structure:
<SavingAccounts>
<SavingAccount>
<ServiceOnline>yes</ServiceOnline>
<ServiceViaPhone>no</ServiceViaPhone>
</SavingAccount>
<SavingAccount>
<ServiceOnline>no</ServiceOnline>
<ServiceViaPhone>yes</ServiceViaPhone>
</SavingAccount>
</SavingAccounts>
What I need to do is filter the 'SavingAccount' nodes using XPATH where the value of 'ServiceOnline' is 'yes' or the value of 'ServiceViaPhone' is yes.
The XPATH should return me two rows!! I can filter 'SavingAccount' nodes where both of the element values are yes like the following XPATH sample, but what I want to do is an or element value comparison???
/SavingAccounts/SavingAccount/ServiceOnline[text()='yes']/../ServiceViaPhone[text()='yes']/..
This is a very fundamental XPath feature: composing a number of conditions with the logical operators and, or, and the function not().
and has a higher priority than or and both operators have lower priority than the relational and equality operators (=, !=, >, >=, < and <=).
So, it is safe to write: A = B and C = D
Some most frequent mistakes made:
People write AND and/or OR. Remember, XPath is case-sensitive.
People use the | (union) operator instead of or
Lastly, here is my solution:
/SavingAccounts/SavingAccount
[ServiceOnLine='yes' or ServiceViaPhone='yes']
/SavingAccounts/SavingAccount[(ServiceOnLine='yes') or (ServiceViaPhone='yes')]
Will
/SavingAccounts/SavingAccount[ServiceOnline/text()='yes' or ServiceViaPhone/text()='yes']
do the trick?
I have no XPath evaluator handy at the moment.
EDIT:
If I remember correctly, you don't need the text(), so
[ServiceOnline='yes' or ServiceViaPhone='yes']
should be sufficient, and more readable.
EDIT:
Yes, of course, 'or' for predicate expressions, my bad.