通过两个属性值VBS选择xml节点

时间:2016-06-22 14:17:52

标签: xml vbscript

我无法找到答案。我正在编写VBScript以与xml文件进行交互。

我尝试使用selectSingleNode根据两个不同的属性值检索xml节点,例如

set featureNode=objXMLDoc.SelectSingleNode("//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE', @section='E-E']")

我试图选择具有属性ID =" N ANGLE"和section =" E-E"

的要素节点

我收到错误"预期令牌"]"发现",""显然它不喜欢逗号。有没有人知道正确的语法(如果可能的话)?

这是我的xml:

<Sevo>
    <NCR ID="N1" PN="30G3955">
        <SN ID="006302" Op="millvane" ncrDate="6/22/2016 8:43:36 AM" mrbDate="">
            <FT ID="N ANGLE" Section="E-E" LSL="32.243" USL="32.909">32.240</FT> 
            <FT ID="N ANGLE" Section="L-L" LSL="25.336" USL="26.002">25.300</FT> 
        </SN>
    </NCR>
</Sevo>

最糟糕的情况我可以使用getElementsByTagName得到我想要的东西并循环直到找到节点。

由于

1 个答案:

答案 0 :(得分:0)

逻辑连接称为“和”(doc)。演示:

Option Explicit

Dim s : s = Join(Array( _
    "<Sevo>" _
  ,     "<NCR ID=""N1"" PN=""30G3955"">" _
  ,         "<SN ID=""006302"" Op=""millvane"" ncrDate=""6/22/2016 8:43:36 AM"" mrbDate="""">" _
  ,             "<FT ID=""N ANGLE"" Section=""E-E"" LSL=""32.243"" USL=""32.909"">32.240</FT>" _
  ,             "<FT ID=""N ANGLE"" Section=""L-L"" LSL=""25.336"" USL=""26.002"">25.300</FT>" _
  ,         "</SN>" _
  ,     "</NCR>" _
  , "</Sevo>" _
), "")

Dim oXDoc  : Set oXDoc = CreateObject("MSXML2.DomDocument.6.0")
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.loadXML s

If 0 = oXDoc.ParseError Then
   WScript.Echo "looks ok"
   Dim sXPath
'    does not 'work' ==>
'      "//Sevo/NCR[@ID='N1']/SN[@ID='006302']/feature[@ID='N ANGLE', @section='E-E']" _
   For Each sXpath In Array( _
       "//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE']" _
     , "//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@Section='E-E']" _
     , "//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE' and @Section='E-E']" _
     , "//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE' and @Section='L-L']" _
   )
       WScript.Echo "search for |" & sXPath & "|"
       Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXpath)
       If Not ndFnd Is Nothing Then
          WScript.Echo "     found |" & ndFnd.xml & "|"
       Else
          WScript.Echo "     nothing found"
       End If
   Next
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

输出:

cscript 37970769.vbs
looks ok
search for |//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE']|
     found |<FT ID="N ANGLE" Section="E-E" LSL="32.243" USL="32.909">32.240</FT>|
search for |//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@Section='E-E']|
     found |<FT ID="N ANGLE" Section="E-E" LSL="32.243" USL="32.909">32.240</FT>|
search for |//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE' and @Section='E-E']|
     found |<FT ID="N ANGLE" Section="E-E" LSL="32.243" USL="32.909">32.240</FT>|
search for |//Sevo/NCR[@ID='N1']/SN[@ID='006302']/FT[@ID='N ANGLE' and @Section='L-L']|
     found |<FT ID="N ANGLE" Section="L-L" LSL="25.336" USL="26.002">25.300</FT>|