xml - 尝试在 VBScript 中使用 XPATH 解析 XML 时获取空值

标签 xml xslt xpath vbscript xml-parsing

我是 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 中的值。

最佳答案

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

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

但是,由于您在代码中设置了与 XPath 一起使用的 namespace ,因此这应该更加简洁明了:
"//RegisterAndInviteParticipantResponse/
   RegisterAndInviteParticipantResult/
   a:AccessCode"

这是 @ 时的示例可以与原始文档一起使用(假设 o 前缀也将在代码中显式绑定(bind)):
"/s:Envelope/s:Header/o:Security/@s:mustUnderstand"

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

关于xml - 尝试在 VBScript 中使用 XPATH 解析 XML 时获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44450891/

相关文章:

xml - R getNodeSet中的部分匹配

perl - 修复在 XML::Twig 中使用的 XPath 谓词

iphone - 在iPhone上保存/解析XML的最佳方法

XSLT 排序还是分组?

java - 如何使用 Java 从 ATOM 提要中提取 XHTML?

xml - 为什么我的 XSLT 输出中有额外的文本?

xml - XSL : Xpath Query in <xsl:apply-templates> Isn't Returning Any Results

xml - XPATH查询使用xml中的值

android - 无法从警报对话框中的 EditText View 获取数据

xml - 从命令行修改 XML