【问题标题】:Getting null values when trying to parse an XML using XPATH in VBScript尝试在 VBScript 中使用 XPATH 解析 XML 时获取空值
【发布时间】:2017-06-09 06:36:49
【问题描述】:

我是 xPath 的新手,我正在尝试使用 VbScript 中的 XPath 解析下面给出的 XML,但我总是得到空值。

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-
utility-1.0.xsd">
   <s:Header>
      <o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-
200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1">
         <u:Timestamp u:Id="_0">
            <u:Created>2017-03-30T07:08:12.264Z</u:Created>
            <u:Expires>2017-03-30T07:13:12.264Z</u:Expires>
         </u:Timestamp>
      </o:Security>
   </s:Header>
   <s:Body>
      <RegisterAndInviteParticipantResponse 
xmlns="http://www.cubonline.com/CubOnline/WebServices/201207">
         <RegisterAndInviteParticipantResult xmlns:a="http://schemas.datacontract.org/2004/07/Cub.CubOnline.WebServices.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:UserID>322988</a:UserID>
        <a:AccessCode>7EBECBBD</a:AccessCode>
        <a:LogonUrl>https://uat.sonline.com/Online/Standalone/PLogon.aspx?skin=1379+751&amp;lang=en-GB</a:LogonUrl>
        <a:AuthenticatedURL i:nil="true" />
        <a:ReportLinksURL>https://uat.sonline.com/Online/Standalone/XViewReportLinks.aspx?key=091d1e-6cd0-45fd-8c81-61ab70107f34&amp;hash=7DDEDAF4CCDD47E5880F086C62E660F8F45B2C9E&amp;skin=234</a:ReportLinksURL>
        <a:ParticipantScheduleID>791777</a:ParticipantScheduleID>
     </RegisterAndInviteParticipantResult>
  </RegisterAndInviteParticipantResponse>
   </s:Body>
</s:Envelope>

下面是我的 VBScript 代码:

Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc.async = False
xmlDoc.resolveExternals = False
xmlDoc.validateOnParse = False


If Not xmlDoc.loadXML(registerParticipantResponse) Then
'//..........do something.
End If

'//=====================================================
'//Try Xpath'ing a few values
'//=====================================================
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.setProperty "SelectionNamespaces", 
"xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns=http://www.cubonline.com/CubOnline/WebServices/201207' xmlns:a='http://schemas.datacontract.org/2004/07/Cub.CubOnline.WebServices.DataContracts' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'"
Dim currNode


Set currNode = xmlDoc.selectSingleNode("//*[local-
name()='RegisterAndInviteParticipantResponse']/@*[local-
name()='RegisterAndInviteParticipantResult']/@*[local-name()='AccessCode']")
accessCode = ""
If Not currNode Is Nothing Then
accessCode = currNode.text
End If

注意:我得到的“访问码”为空。 所以请帮助我如何通过Xpath访问accesscode中的值。

【问题讨论】:

    标签: xml xslt xpath vbscript xml-parsing


    【解决方案1】:

    @ 字符应该从 XPath 中取出,因为它们不是属性而是元素:

    "//*[local-name()='RegisterAndInviteParticipantResponse']/
       *[local-name()='RegisterAndInviteParticipantResult']/
       *[local-name()='AccessCode']"
    

    但是,由于您在代码中设置了与 XPath 一起使用的命名空间,因此应该更加简洁明了:

    "//RegisterAndInviteParticipantResponse/
       RegisterAndInviteParticipantResult/
       a:AccessCode"
    

    下面是@ 可以与原始文档一起使用的示例(假设o 前缀也将在代码中显式绑定):

    "/s:Envelope/s:Header/o:Security/@s:mustUnderstand"
    

    一般来说,当 XPath 没有找到具有指定名称的元素或属性时,它不会返回错误,而是返回一个空序列。

    【讨论】:

    • 我不知道这个特定的 XPath 引擎,但如果它与 XPath 1.0 一致,那么无前缀元素名称总是指无命名空间中的名称。所以需要有一个前缀与RegisterAndInviteParticipantResponse一起使用并绑定到正确的命名空间
    • 啊,是的,很好,我忘记了关于 XPath 1.0 的事情,而且很可能 VBScript 仍然使用这个旧版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2012-04-07
    • 2019-06-22
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多