check xml node is exist or not using vbscript
这是 XML 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | <ECSC> <ATTRIBUTES> <some part of attribute section> </ATTRIBUTES> <ETXML_LINE_TABTYPE> <some part of script section> </ETXML_LINE_TABTYPE> <PARAMETERS> <ETPAR_GUIX> <item> <PNAME>I_LANG</PNAME> <PTYP>I</PTYP> <PINDEX>0003</PINDEX> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0001</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_1</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0009</PINDEX> <PGROUP>SAPGUI</PGROUP> <XMLREF_TYP>G</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>SAPGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0001</SORT_LNR> <PREF_NAME2>SAPGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>V_LANG</PNAME> <PTYP>V</PTYP> <PINDEX>0007</PINDEX> <PGROUP>V.01</PGROUP> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0002</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_2</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0010</PINDEX> <PGROUP>SAPGUI</PGROUP> <XMLREF_TYP>G</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>SAPGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0002</SORT_LNR> <PREF_NAME2>SAPGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>I_AGENT</PNAME> <PTYP>I</PTYP> <PINDEX>0002</PINDEX> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0003</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_3</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0011</PINDEX> <PGROUP>GETGUI</PGROUP> <XMLREF_TYP>I</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>GETGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0003</SORT_LNR> <PREF_NAME2>GETGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>I_TYPE</PNAME> <PTYP>I</PTYP> <PINDEX>0004</PINDEX> <PGROUP>V.04</PGROUP> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0004</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_4</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0012</PINDEX> <PGROUP>GETGUI</PGROUP> <XMLREF_TYP>I</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>GETGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0004</SORT_LNR> <PREF_NAME2>GETGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>V_AGENT</PNAME> <PTYP>V</PTYP> <PINDEX>0006</PINDEX> <PGROUP>I.02</PGROUP> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0005</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_5</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0013</PINDEX> <PGROUP>GETGUI</PGROUP> <XMLREF_TYP>I</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>GETGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0005</SORT_LNR> <PREF_NAME2>GETGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>V_TYPE</PNAME> <PTYP>V</PTYP> <PINDEX>0008</PINDEX> <PGROUP>V.03</PGROUP> <PDATLEN>0128</PDATLEN> <PINTTYP>C</PINTTYP> <PINTLEN>000128</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0006</SORT_LNR> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>WE20_100_STEP_6</PNAME> <PTYP>X</PTYP> <PDESC>WE20 - SAPMSEDIPARTNER - 100</PDESC> <PINDEX>0014</PINDEX> <PGROUP>SAPGUI</PGROUP> <XMLREF_TYP>G</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>SAPGUI</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0006</SORT_LNR> <PREF_NAME2>SAPGUI</PREF_NAME2> <VALUE><VALUE></VALUE> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>MSG_1</PNAME> <PTYP>X</PTYP> <PDESC>Rules for Message Check eCATT Command MESSAGE</PDESC> <PINDEX>0005</PINDEX> <PGROUP>MESSAGE</PGROUP> <XMLREF_TYP>M</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>ETMSG_DEF_TABTYPE</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0007</SORT_LNR> <PREF_NAME2>ETMSG_DEF_TABTYPE</PREF_NAME2> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>E_MSG_1</PNAME> <PTYP>X</PTYP> <PDESC>Collected Messages Before ENDMESSAGE</PDESC> <PINDEX>0001</PINDEX> <PGROUP>ENDMESSAGE</PGROUP> <XMLREF_TYP>N</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>ETMSG_RES_TABTYPE</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0008</SORT_LNR> <PREF_NAME2>ETMSG_RES_TABTYPE</PREF_NAME2> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> <item> <PNAME>ZX_FI_FP_0569_MS07_COAS_FB_1</PNAME> <PTYP>X</PTYP> <PDESC>TF_FI_FP_FI_0569_MS07_CO_Search_Help_Internal_Orders_vTD0_1_</PDESC> <PINDEX>0015</PINDEX> <PGROUP>REF</PGROUP> <XMLREF_TYP>R</XMLREF_TYP> <PSTRUC_TYP>T</PSTRUC_TYP> <PREF_NAME>ZX_FI_FP_0569_MS07_COAS_FB01</PREF_NAME> <PDATLEN>0000</PDATLEN> <PINTLEN>000000</PINTLEN> <PDECIMALS>000000</PDECIMALS> <SORT_LNR>0009</SORT_LNR> <PREF_NAME2>ZX_FI_FP_0569_MS07_COAS_FB01</PREF_NAME2> <VAL_TYPE>T</VAL_TYPE> <TAB_INDEX>0</TAB_INDEX> </item> </ETPAR_GUIX> |
从上面的 XML 文件中,我们要验证以下条件:
1) 来自
2) 来自
不存在
这是我尝试过的 vbscript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("C:\ ewXML.xml") Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument") objMSXML.setProperty"SelectionLanguage","XPath" objMSXML.async = False objMSXML.load sFSpec objMSXML.validateOnParse = True dim item,items,root counter=0 Set NodeList = objMSXML.documentElement.selectNodes("/ECSC/PARAMETERS/ETPAR_GUIX/item") for each item in NodeList myPNAME = objMSXML.getElementsByTagName("PNAME").item(counter).text myPTYP = objMSXML.getElementsByTagName("PTYP").item(counter).text myPGROUP = objMSXML.getElementsByTagName("PGROUP").item(counter).text If (Left(myPNAME, 2) ="I_") Then IsValid_I = True If (Left(myPTYP, 1) <>"I") Then IsValid_I = False End If Set pgroup = objMSXML.selectSingleNode("/ECSC/PARAMETERS/ETPAR_GUIX/item[not(PGROUP)]/PNAME") If Not pgroup Is Nothing Then msgbox("PGROUP is exist for" &myPNAME) If (Left(myPGROUP, 1) <>"I" )Then IsValid_I = False End If Else msgbox("PGROUP is not exist for"&myPNAME) End If If IsValid_I = False Then msgbox(myPNAME &" is not valid.") End If IsValid_I = True End If If (Left(myPNAME, 2) ="V_") Then IsValid_V = True If (Left(myPTYP, 1) <>"V") Then IsValid_V = False End If Set pgroup = objMSXML.selectSingleNode("/ECSC/PARAMETERS/ETPAR_GUIX/item[not(PGROUP)]/PNAME") If Not pgroup Is Nothing Then msgbox("PGROUP is exist for" &myPNAME) If (Left(myPGROUP, 1) <>"V" )Then IsValid_V = False End If Else msgbox("PGROUP is not exist for"&myPNAME) End If IsValid_V = False Then msgbox(myPNAME &" is not valid.") End If IsValid_V = True End If If (Left(myPNAME,2) ="E_") Then IsValid_E = True If (Left(myPTYP, 1) <>"E") Then IsValid_E = False End If Set pgroup = objMSXML.selectSingleNode("/ECSC/PARAMETERS/ETPAR_GUIX/item[not(PGROUP)]/PNAME") If Not pgroup Is Nothing Then msgbox("PGROUP is exist for" &myPNAME) If (Left(myPGROUP, 1) <>"E" )Then IsValid_E = False End If Else msgbox("PGROUP is not exist for"&myPNAME) End If If IsValid_E = False Then msgbox(myPNAME &" is not valid.") End If IsValid_E = True End If counter=counter+1 next |
请帮帮我。提前致谢。
使用 XPath 和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | Option Explicit Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") Dim sFSpec : sFSpec = oFS.GetAbsolutePathName("C:\\doc.xml") Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument") objMSXML.setProperty"SelectionLanguage","XPath" objMSXML.async = False objMSXML.load sFSpec objMSXML.validateOnParse = True FindMissingPgroups objMSXML FindInvalidPtyps objMSXML FindInvalidPGroups objMSXML Sub FindMissingPgroups(doc) Dim query, items, item, pname query ="/ECSC/PARAMETERS/ETPAR_GUIX/item[not(PGROUP)]" Set items = doc.selectNodes(query) For Each item In items Set pname = item.selectSingleNode("PNAME") MsgBox"Missing PGROUP for" & pname.text Next ' item End Sub Sub FindInvalidPtyps(doc) Dim query, items, item, pname, ptyp query = query &"/ECSC/PARAMETERS/ETPAR_GUIX/item[PGROUP][" query = query &" (starts-with(PNAME, 'I_') and not(starts-with(PTYP, 'I'))) or" query = query &" (starts-with(PNAME, 'V_') and not(starts-with(PTYP, 'V'))) or" query = query &" (starts-with(PNAME, 'E_') and not(starts-with(PTYP, 'E')))" query = query &"]" Set items = doc.selectNodes(query) For Each item In items Set pname = item.selectSingleNode("PNAME") Set ptyp = item.selectSingleNode("PTYP") MsgBox"Invalid PTYP (" & ptyp.text &") for" & pname.text Next ' item End Sub Sub FindInvalidPgroups(doc) Dim query, items, item, pname, pgroup query = query &"/ECSC/PARAMETERS/ETPAR_GUIX/item[PGROUP][" query = query &" (starts-with(PNAME, 'I_') and not(starts-with(PGROUP, 'I'))) or" query = query &" (starts-with(PNAME, 'V_') and not(starts-with(PGROUP, 'V'))) or" query = query &" (starts-with(PNAME, 'E_') and not(starts-with(PGROUP, 'E')))" query = query &"]" Set items = doc.selectNodes(query) For Each item In items Set pname = item.selectSingleNode("PNAME") Set pgroup = item.selectSingleNode("PGROUP") MsgBox"Invalid PGROUP (" & pgroup.text &") for" & pname.text Next ' item End Sub |
一些注意事项:
-
始终在每个脚本的顶部使用
Option Explicit 。它将在将来为您省去头疼的问题。 - 我将测试分成三组而不是两组。分别测试 PTYP 和 PGROUP 更容易。
-
请注意,无效 PTYP 和 PGROUP 的测试包括谓词
[PGROUP] 。这确保我们只检查具有 PGROUP 的项目(这是条件 2 要求的一部分)。
您可以使用两个
对于第一条规则,选择所有
1 2 3 4 5 6 7 8 9 10 11 12 | Set nodesWithPgroup = objMSXML.documentElement.selectNodes("/ECSC/PARAMETERS/ETPAR_GUIX/item[PGROUP]") For Each item in nodesWithPgroup myPNAME = item.SelectSingleNode("PNAME").text myPTYP = item.SelectSingleNode("PTYP").text myPGROUP = item.SelectSingleNode("PGROUP").text code = Left(myPNAME, 1) If (Left(myPNAME, 2) = code &"_") Then If (Left(myPTYP, 1) <> code Or Left(myPGROUP, 1) <> code) Then msgbox(myPNAME &" is not valid.") End If End If Next |