unknown Method in xPath run by VBScript

无人久伴 提交于 2019-12-21 20:19:11

问题


the error message is

msxml3.dll: Unknown method.

/Record/CelloXml/Integration/Case/ServiceEvent[-->last()<--]/Service/Comment

My code looks like this and the error is on the case NEW

Case OLD works fine with [0] in there

'On Error Resume Next

Public Function GetParameterXml()
 GetParameterXml = _
 "<Parameters>" &_
  "<Parameter Value='Age' Code='A' Description='Age' Type='Combo' Tooltip='Would you like the newest or oldest Reason?'>" &_
   "    <Options>" &_
    " <Option Code='O' Description='Oldest' Value='OLD' />" &_
    " <Option Code='N' Description='Newest' Value='NEW' />" &_
   "    </Options>" &_
  "</Parameter>" &_
 "</Parameters>"
End Function

'Parameter Variables
Dim Age : Set Age = Parameters.Item( Bookmark , "Age" )

' PlaceHolder Variables
Dim CommentNodes

''' stop here and look around
stop

Select Case Age.Value
 Case "OLD":
  Set CommentNodes = XmlDoc.SelectNodes("Record/CelloXml/Integration/Case/ServiceEvent[0]/Service/Comment")
 Case "NEW":
  Set CommentNodes = XmlDoc.SelectNodes("Record/CelloXml/Integration/Case/ServiceEvent[last()]/Service/Comment")
End Select

For Each CommentNode In CommentNodes
 ReturnData = ReturnData & CommentNode.Text & MD
Next

If Len(ReturnData) > 0 Then
 ReturnData = Left(ReturnData, Len(ReturnData) - Len(MD))
Else
 ReturnData = vbNullString 
End If

for some reason it doesn't like last() is there another way to do this? I need the functionality of the last function, so that if there is only one ServiceEvent node it will still grab that node.


I am not a VBScript Guy (yet) and everything that I have learned about xPath is what I have learned on the job.


回答1:


Just add the following property

XmlDoc.SetProperty "SelectionLanguage", "XPath"

My test xml is as follows

<root>
    <child1>
        <child2>
            <child3>test1</child3>
        </child2>
        <child2>
            <child3>test2</child3>
        </child2>
        <child2>
            <child3>test3</child3>
        </child2>
        <child2>
            <child3>test4</child3>
        </child2>
    </child1>
</root>

My test code is as follows

strXMLReadFile = "C:\Test.xml"

Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.SetProperty "SelectionLanguage", "XPath"
xmlDoc.Async = False
xmlDoc.Load(strXMLReadFile)

Set nodeXML = xmlDoc.SelectNodes("//root/child1/child2[last()]")
msgbox nodeXML(0).Text

I get test4




回答2:


SomeNode[position() = last()]

is what you want.



来源:https://stackoverflow.com/questions/20617236/unknown-method-in-xpath-run-by-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!