I have two Xpath expressions ...
//*[#id='gutter']/p[strong[text()='Date:']]/text()
//*[#id='gutter']/p[strong[text()='Time:']]/text()
How do I write a single xpath expression that combines the two above and would return the same node-set as combining the results of running each of the expressions above individually?
In general this Xpath expression:
expr1 | expr2
selects the union of all nodes selected by expr1 and all nodes selected by expr2.
The | character denotes the XPath union operator.
You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.
In this concrete case:
//*[#id='gutter']/p[strong[text()='Date:']]/text()
|
//*[#id='gutter']/p[strong[text()='Time:']]/text()
While this expression can be optimized, it has the advantage that the union operator "works" in all such cases, can be expressed almost mechanically, saves time and eliminates the possibility for introducing error by a more complicated refactoring.
How about:
//*[#id='gutter']/p[strong[text()='Date:' or text()='Time:']]/text()
which is more or less self-explanatory.
Related
I am trying to write an algorithm for presenting the below mathematical expression in a binary tree in order to present the post fixed and prefixed expression.
I know the precedence levels of common used operators, and i know how to deal with normal mathematical expressions, but i am not familiar with the use of inequalities like < >= , add for that the use of AND.
any help will be appreciated
Operator precedence usually goes
arithmetic > equality > logical
so the < and >= would evaluate before the AND.
Treat them like normal arithmetic operators when building the parse tree but give equality operators lower precendence than arithmetic, and logical lower than that.
for example check the java operator precendence
According to Wikipedia the shunting-yard algorithm is used to parse mathematical expressions. But is there any reason it could not be used with a mix of logical and arithmetic expressions, as well as comparisons?
As an example, could one use it to parse this:
a+b<17 && a+b>3 || a==b
As far as I can see, you could just define logical operators to have the lowest precedence, followed by comparison operators, and then have the usual arithmetic operators with increasing precedence. Or am I missing something?
I have to write a program that tests whether two algebraic expressions are equivalent. It should follow MDAS precedence and parenthesis grouping. To solve the problem about precedence, I'm thinking I should implement a Infix to Postfix Notation converter for these expressions. But by doing this, I could not conclude their equivalence.
The program should look like this:
User Input: a*(a+b) = a*a + a*b
Output : Equivalent
For this problem I'm not allowed to use Computer Algebraic Systems or any external libraries. Please don't post the actual code if you have one, I just need an idea to work this problem out.
If you are not allowed to evaluate the expressions, you will have to parse them out into expression trees.
After that, I would get rid of all parenthesis by multiplying/dividing all members so a(b - c) becomes a*b - a*c.
Then convert all expressions back to strings, making sure you have all members alphabetically sorted (a*b, not b*a) ,remove all spaces and compare strings.
That's an idea:
You need to implement building expression tree first because it's a very natural representation of expression.
Then maybe you'll need to simplify it by open brackets and etc. using associative or distributive algebraic properties.
Then you'll have to compare trees. It's not obvious because you need to take care of all branch permutations in commutative operations and etc. E.g. you can sort them (I mean branches) and then compare for equality. Also you need to keep in mind possible renaming of parameters, i.e. a + b need to be equal x + y.
I am studying on Ivan Bratko book: "Programming for artificial intelligence"
Now I am studying the operators and I have some doubts about it, on the book I can read the following thing:
So the precedence of the operators decides what is the correct interpretation of expressions. For example, the expression a + b*c can be, in principle, understood either as:
1. +(a, *(b,c))
or as:
2. *(+(a,b), c)
And now I havde the first doubt: "what it means this thing? it seems me very strage because these two expressions give different three and different result !!!
For example if I have: a=2, b=3, c=4
The result of the first one is 14 and the result of the second one is 20 so there are different: different thress means different order of operator execution that means different result !!!
So I think that (using the usual priority of arithmetic operator: execute first the multiplications and after the sums) the correct expression is the first one and the second one is wrong.
Is it correct?
Continuing to read the book I can read also:
The general rule is that operator with the highest precedence is the principal functor of the term. If expression containing + and * are to be understood according to our normal convention, then + have a higher precedence then * operator
and now I have the second doubt: as I said, in normal convention of arithmetic I execute first the multiplications and after the sums,so in my opinion is the * operator that have the precedence and not +
What I am missing about it? Why on the book say that + has higher precedence then *?
"The principal functor of the term" means the last operation to be executed, or the outermost one in prefix notation. This definition is the inverse of yours, thus the contradiction.
So the precendence of the operators decides what is the correct interpretation of expressions. For example, the expression a + b*c can be, in principle, understood either as:
+(a, *(b,c))
or as:
*(+(a,b), c)
It says that without precedence, those are 2 possible ways to interpret the expression (and different way of parsing the expression will give different result). You only know to group a + (b * c) when you know that * should be executed before +. (I avoid explaining with precedence here, since it is confusing as pointed out below).
+ and * are just symbols, and it just happens that they are used as operator with some defined precedence. Generally speaking, you can define anything to be an operator, and give it a precedence.
The general rule is that operator with the highest precedence is the principal functor of the term. If expression containing + and * are to be understood according to our normal convention, then + have a higher precedence than * operator
The definition of precedence in English is "The condition of being considered more important than someone or something else; priority in importance, order, or rank". As long as something has to happen before some other thing, we have a precedence.
In C, operator precedence is the order of binding in an expression. Higher precedence operator in C will get executed before lower precedence operators.
In Prolog, the precedence is the order of getting the functor, which is the reverse of the case of C operator's order of binding. Higher precedence operator will appear earlier when we analyze the expression with =...
anyone have idea how to solve this problem
counts the number of occurrences of an operator inside an expression. For instance, the query:
?- count(a+b*c-(2+3*4)/(5*(2+a)+(b+c)^f((d-e)*(x-y))), *, C).
would count the number of occurrences of operator * in the expression given as the first argument and output on C
I am using SWI-prolog
Is this homework?
Here's some hints:
Prolog operators are syntactic sugar around normal prolog terms. The expression 3 * 2 + 1 is parsed as the term '+'('*'(3,2),1).
The built-in predicate =.. decomposes a term into a list, the head of which is the functor and the tail of which comprises the [non-decomposed] terms that are the arguments to the original term.
The built-in predicate functor/3 unifies a term with its functor and arity.
You might also want to look at arg/3 which provide the means to examine the arguments of the specified term by ordinal position.
Now that you know that, a fairly simple recursive solution should present itself. If you need to factor in the arity of the desired operator, it's a little more convoluted (but not much).