XML XPath syntax Summary

Source: Internet
Author: User
Tags xml xpath

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

  1. The most common XML data types are: element, attribute, comment, text.
  2. Element, which indicates the <Name> Tom <Name> node. It can include: element, text, comment, processinginstruction, CDATA, and entityreference.
  3. Attribute, which is the bold part in <employee>.
  4. Comment, pointing to: <! -- My comment --> node.
  5. Text, which is in bold of <Name> Tom <Name>.
  6. In XML, you can use xmlnode objects to refer to various XML data types.
  7. 2.1 query nodes (sets) with known absolute paths)
  8. Objnodelist = objdoc. selectnodes ("Company/department/employees/employee ")
  9. Or
  10. Objnodeobjnodelist = objnode. selectnodes ("/Company/department/employees/employee ")
  11. 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:
  12. Objnodeobjnode = objnode. selectsinglenode ("/Company/department/employees/employee ")
  13. If not (objnode is nothing) then
  14. '-Do Process
  15. End if
  16. 2.2 query nodes (sets) with known relative paths)
  17. You can use a relative path similar to the file path to query XML data.
  18. Objnode = objdoc. selectsinglenode ("Company/department ")
  19. Objnodeobjnodelist = objnode. selectnodes ("../Department)
  20. Objnodeobjnode = objnode. selectnode ("employees/employee ")
  21. 2.3 query nodes (sets) with known element names)
  22. 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:
  23. Objnodelist = objdoc. selectnodes ("company // employee ")
  24. 2.4 query attribute nodes
  25. 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:
  26. Objnodelist = objdoc. selectnodes ("Company/department/employees/employee/@ ID ")
  27. Objnodelist = objdoc. selectnodes ("company // @ ID ")
  28. 2.5 query text nodes
  29. Use text () to obtain the text node.
  30. Objnode = objdoc. selectsinglenode ("Company/department/deparmt_name/text ()")
  31. 2.6 query nodes with specific conditions
  32. Use the [] symbol to query nodes with specific conditions. For example:
  33. A. Return the employee node with ID 10102.
  34. Objnode = objdoc. selectsinglenode ("Company/department/employees/employee [@ ID = '000000']")
  35. B. Return the Name node whose name is Zhang Qi.
  36. Objnode = objdoc. selectsinglenode ("Company/department/employees/employee/name [text () = 'zhang qi']")
  37. C. Return the Department Name node with 22345 employees
  38. Objnode = objdoc. selectsinglenode ("Company/Department [employees/employee/@ ID = '000000']/department_name ")
  39. 2.7 query nodes in multiple modes
  40. Use the | symbol to obtain nodes in multiple modes. For example:
  41. Objnodelist = objdoc. selectnodes ("Company/department/department_name | Company/department/manager ")
  42. 2.8 Query any subnode
  43. You can use the * symbol to return all the subnodes of the current node.
  44. Objnodelist = objdoc. selectnodes ("Company/*/Manager)
  45. Or
  46. Objnodeobjnodelist = objnode. childnodes
  47. 3. Edit XML data
  48. 3.1 add an attribute node of an element
  49. Dim objnodeattr as xmlnode
  50. Objnodeattr = objdoc. createattribute ("ID", nothing)
  51. Objnodeattr. innerxml = "101"
  52. Objnode. Attributes. append (objnodeattr)
  53. 3.2 delete attributes of an element
  54. Objnode. Attributes. Remove (objnodeattr)
  55. 3.3 Add a child element)
  56. Dim objnodechild as xmlnode
  57. Objnodechild = objdoc. createelement (nothing, "ID", nothing)
  58. Objnodechild. innerxml = "101"
  59. Objnode. appendchild (objnodechild)
  60. 3.4 delete a child element
  61. Objnode. removechild (objnodechild)
  62. 3.5 replace a child element
  63. Objnode. replaceChild (newchild, oldchild)
  64. 4. Reference Data
  65. <? XML version = "1.0" encoding = "UTF-8"?>
  66. <Company>
  67. <Department>
  68. <Department_name> Cai wubu </department_name>
  69. <Manager> Zhang Bin </Manager>
  70. <Employees>
  71. <Employee>
  72. <Employee_id> 12345 </employee_id>
  73. <Name> Zhang Bin </Name>
  74. <Gender> male </gender>
  75. </Employee>
  76. <Employee>
  77. <Employee_id> 10101 </employee_id>
  78. <Name> Zhang Qi </Name>
  79. <Gender> female </gender>
  80. </Employee>
  81. <Employee>
  82. <Employee_id> 10102 </employee_id>
  83. <Name> zhang xia </Name>
  84. <Gender> male </gender>
  85. </Employee>
  86. <Employee>
  87. <Employee_id> 10201 </employee_id>
  88. <Name> zhangchuang </Name>
  89. <Gender> male </gender>
  90. </Employee>
  91. <Employee>
  92. <Employee_id> 10202 </employee_id>
  93. <Name> Zhang Jun </Name>
  94. <Gender> male </gender>
  95. </Employee>
  96. </Employees>
  97. </Department>
  98. <Department>
  99. <Department_name> kaifa Bu </department_name>
  100. <Manager> Wang Bin </Manager>
  101. <Employees>
  102. <Employee>
  103. <Employee_id> 22345 </employee_id>
  104. <Name> Wang Bin </Name>
  105. <Gender> male </gender>
  106. </Employee>
  107. <Employee>
  108. <Employee_id> 20101 </employee_id>
  109. <Name> Wang Qi </Name>
  110. <Gender> female </gender>
  111. </Employee>
  112. <Employee>
  113. <Employee_id> 20102 </employee_id>
  114. <Name> Wang Xia </Name>
  115. <Gender> male </gender>
  116. </Employee>
  117. <Employee>
  118. <Employee_id> 20201 </employee_id>
  119. <Name> Wang Chuang </Name>
  120. <Gender> male </gender>
  121. </Employee>
  122. <Employee>
  123. <Employee_id> 20201 </employee_id>
  124. <Name> Wang Jun </Name>
  125. <Gender> male </gender>
  126. </Employee>
  127. </Employees>
  128. </Department>
  129. </Company>

URL: http://www.cnblogs.com/greatverve/archive/2011/09/30/xpath.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.