I just encountered a multi-query xmldoc. selectsinglenode ("root/element [@ name = 'Big meteorology '] [@ age = '30']")
Query xmldoc. selectsinglenode Based on innertext ("root/element [text () = 'Big meteorology ']")
By the way, collect and summarize.
XPath usage in the selectsinglenode method in XML
- The most common XML data types are: element, attribute, comment, text.
-
- Element, which indicates the <Name> Tom <Name> node. It can include: element, text, comment, processinginstruction, CDATA, and entityreference.
-
- Attribute, which is the bold part in <employee>.
-
- Comment, pointing to: <! -- My comment --> node.
-
- Text, which is in bold of <Name> Tom <Name>.
-
- In XML, you can use xmlnode objects to refer to various XML data types.
-
- 2.1 query nodes (sets) with known absolute paths)
-
- Objnodelist = objdoc. selectnodes ("Company/department/employees/employee ")
-
- Or
-
- Objnodeobjnodelist = objnode. selectnodes ("/Company/department/employees/employee ")
-
- The preceding two methods return a nodelist object. to return a single node, use the selectsinglenode method. If one or more nodes are queried by this method, the first node is returned; if no node is queried, nothing is returned. For example:
-
- Objnodeobjnode = objnode. selectsinglenode ("/Company/department/employees/employee ")
-
- If not (objnode is nothing) then
-
- '-Do Process
-
- End if
-
- 2.2 query nodes (sets) with known relative paths)
-
- You can use a relative path similar to the file path to query XML data.
-
- Objnode = objdoc. selectsinglenode ("Company/department ")
-
- Objnodeobjnodelist = objnode. selectnodes ("../Department)
-
- Objnodeobjnode = objnode. selectnode ("employees/employee ")
-
- 2.3 query nodes (sets) with known element names)
-
- When using an irregular hierarchical document, you can use the // symbol to query its child, sun, or all other elements in the multi-level document because you do not know the element name of the intermediate level. For example:
-
- Objnodelist = objdoc. selectnodes ("company // employee ")
-
- 2.4 query attribute nodes
-
- All the above methods return element nodes (sets) and attribute. You only need to use the corresponding method and add a @ symbol before the attribute name. For example:
-
- Objnodelist = objdoc. selectnodes ("Company/department/employees/employee/@ ID ")
-
- Objnodelist = objdoc. selectnodes ("company // @ ID ")
-
- 2.5 query text nodes
-
- Use text () to obtain the text node.
-
- Objnode = objdoc. selectsinglenode ("Company/department/deparmt_name/text ()")
-
- 2.6 query nodes with specific conditions
-
- Use the [] symbol to query nodes with specific conditions. For example:
-
- A. Return the employee node with ID 10102.
-
- Objnode = objdoc. selectsinglenode ("Company/department/employees/employee [@ ID = '000000']")
-
- B. Return the Name node whose name is Zhang Qi.
-
- Objnode = objdoc. selectsinglenode ("Company/department/employees/employee/name [text () = 'zhang qi']")
-
- C. Return the Department Name node with 22345 employees
-
- Objnode = objdoc. selectsinglenode ("Company/Department [employees/employee/@ ID = '000000']/department_name ")
-
- 2.7 query nodes in multiple modes
-
- Use the | symbol to obtain nodes in multiple modes. For example:
-
- Objnodelist = objdoc. selectnodes ("Company/department/department_name | Company/department/manager ")
-
- 2.8 Query any subnode
-
- You can use the * symbol to return all the subnodes of the current node.
-
- Objnodelist = objdoc. selectnodes ("Company/*/Manager)
-
- Or
-
- Objnodeobjnodelist = objnode. childnodes
-
-
-
- 3. Edit XML data
-
- 3.1 add an attribute node of an element
-
- Dim objnodeattr as xmlnode
-
- Objnodeattr = objdoc. createattribute ("ID", nothing)
-
- Objnodeattr. innerxml = "101"
-
- Objnode. Attributes. append (objnodeattr)
-
- 3.2 delete attributes of an element
-
- Objnode. Attributes. Remove (objnodeattr)
-
- 3.3 Add a child element)
-
- Dim objnodechild as xmlnode
-
- Objnodechild = objdoc. createelement (nothing, "ID", nothing)
-
- Objnodechild. innerxml = "101"
-
- Objnode. appendchild (objnodechild)
-
- 3.4 delete a child element
-
- Objnode. removechild (objnodechild)
-
- 3.5 replace a child element
-
- Objnode. replaceChild (newchild, oldchild)
-
-
-
- 4. Reference Data
-
- <? XML version = "1.0" encoding = "UTF-8"?>
-
- <Company>
-
- <Department>
-
- <Department_name> Cai wubu </department_name>
-
- <Manager> Zhang Bin </Manager>
-
- <Employees>
-
- <Employee>
-
- <Employee_id> 12345 </employee_id>
-
- <Name> Zhang Bin </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 10101 </employee_id>
-
- <Name> Zhang Qi </Name>
-
- <Gender> female </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 10102 </employee_id>
-
- <Name> zhang xia </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 10201 </employee_id>
-
- <Name> zhangchuang </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 10202 </employee_id>
-
- <Name> Zhang Jun </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- </Employees>
-
- </Department>
-
- <Department>
-
- <Department_name> kaifa Bu </department_name>
-
- <Manager> Wang Bin </Manager>
-
- <Employees>
-
- <Employee>
-
- <Employee_id> 22345 </employee_id>
-
- <Name> Wang Bin </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 20101 </employee_id>
-
- <Name> Wang Qi </Name>
-
- <Gender> female </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 20102 </employee_id>
-
- <Name> Wang Xia </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 20201 </employee_id>
-
- <Name> Wang Chuang </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- <Employee>
-
- <Employee_id> 20201 </employee_id>
-
- <Name> Wang Jun </Name>
-
- <Gender> male </gender>
-
- </Employee>
-
- </Employees>
-
- </Department>
-
- </Company>
URL: http://www.cnblogs.com/greatverve/archive/2011/09/30/xpath.html