At present, XPath 2.0 has not yet been formally finalized, so the discussion of this article is based on XPath1.0.
XPath supports four basic types:
Node-set
2. String
3. Number
4. Boolean
We know that an Location Step consists of Axis, Node Test and Predicate three parts, and XPath for querying the XML document is composed of several location step, such as / table / row [id = '0000']. Almost always need to use =,! =, <, <=, <= Comparison in Predicate. It is very easy to make people confusion for different objects, especially those involved in Node-Set. For example, Root / NumBers [Integer / @ Value> 4]
Number>
Number>
Root>
Here I explain how to compare different objects in the form of pseudo code. Where CompareObjects involves
Boolean CompareObjects (Object Operand1, Object Operand2, String Operator) Throws Exception {
// Both Objects to Be Compared Are Node-Sets
IF (Both Operand1 and Operand2 Are Node-Sets) {
Iterator I1 = Operand1.iterator ();
Iterator I2 = OPERAND2.ITERATOR ();
While ((node1 = i1.next ()! = null) {
While ((Node2 = I2.Next ()! = null) {
// Convert node1 and node2 to string Values
String s1 = (string) node1;
String s2 = (string) node2;
IF (Comparebasic (S1, S2, Operator) Return True;
}
}
// Neither Object to Be Compared Is A Node-Set
} Else IF (Neither Operand1 Nor Operand2 Is Node-Set) {
Return Comparebasic (Operand1, Operand2, Operator);
} else {
// in this case, one Object is node-set and the other is of basic type.assume operand1 is node-set
Iterator I1 = Operand1.iterator ();
While ((node1 = i1.next ()! = null) {if (operand2 is number) Convert node1 to number as a new object named newoperand1
IF (Operand2 Is String) Convert Node1 To String As a New Object Named NewoPERAND1
IF (Operand2 Is Boolean) Convert Node1 To Boolean As A New Object Named NewoPERAND1
IF (NewoPerand1, Operas2, Operator) Return True
}
}
Return False
}
Boolean CompareBasic (Object Operand1, Object Operand2, String Operator) Throws Exception {
IF (Operator IS "<=" OR "<" or "> =" or ">") {
Convert operand1 and operand2 to number
Compare the two numbers with the operator;
} else IF ("=". Equals (operator) || "=". Equals (operator)) {
IF (at Least One Object Is Boolean) {Convert the Other Object To Boolean}
IF (at Least One Object Is Number) {Convert the Other Object To Number}
IF (at Least One Object Is String) {Convert the Other Object To String}
Compare the new two objects with "=" ory "! ="
} else {
Throw New Exception ("Doesn't Support this Operator!");
}
}
According to the above algorithm, the query is all the
Number>
Number>