使用XmlUtil时,XPath失败(UFT 12.0)

时间:2014-11-12 08:27:03

标签: xpath vbscript xml-parsing hp-uft

给出以下XML:

<?xml version="1.0" encoding="UTF-8" ?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Header>
    <WFContext xmlns="http://service.wellsfargo.com/entity/message/2003/" soapenv:actor="" soapenv:mustUnderstand="0">
      <messageId>cci-sf-dev14.wellsfargo.com:425a9286:14998ac6245:-7e1e</messageId> 
      <sessionId>425a9286:14998ac6245:-7e1d</sessionId> 
      <sessionSequenceNumber>1</sessionSequenceNumber> 
      <creationTimestamp>2014-11-10T00:14:49.243-08:00</creationTimestamp> 
      <invokerId>cci-sf-dev14.wellsfargo.com</invokerId> 
      <activitySourceId>P7</activitySourceId> 
      <activitySourceIdType>FNC</activitySourceIdType> 
      <hostName>cci-sf-dev14.wellsfargo.com</hostName> 
      <billingAU>05426</billingAU> 
      <originatorId>287586861901211</originatorId> 
      <originatorIdType>ECN</originatorIdType> 
      <initiatorId>GTST0793</initiatorId> 
      <initiatorIdType>ACF2</initiatorIdType> 
    </WFContext>
  </soapenv:Header>
  <soapenv:Body>
    <getCustomerInformation xmlns="http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/getCustomerInformation/2012/05/">
      <initiatorInformation xmlns="http://service.wellsfargo.com/provider/ecpr/shared/common/2011/11/">
        <channelInfo>
          <initiatorCompanyNbr xmlns="http://service.wellsfargo.com/entity/message/2003/">114</initiatorCompanyNbr>
        </channelInfo>
      </initiatorInformation>
      <custNbr xmlns="http://service.wellsfargo.com/entity/party/2003/">287586861901211</custNbr>
      <customerViewList xmlns="http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/getCustomerInformationCommon/2012/05/">
        <customerView>
          <customerViewType>GENERAL_INFORMATION_201205</customerViewType>
          <preferences>
            <generalInformationPreferences201205 xmlns="http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/common/2012/05/">
              <formattedNameIndicator xmlns="">true</formattedNameIndicator> 
              <includeTaxCertificationIndicator xmlns="">true</includeTaxCertificationIndicator> 
            </generalInformationPreferences201205>
          </preferences>
        </customerView>
        <customerView>
          <customerViewType>SEGMENT_LIST</customerViewType> 
        </customerView>
        <customerView>
          <customerViewType>LIMITED_PROFILE_REQUIRED_DATA</customerViewType> 
        </customerView>
        <customerView>
          <customerViewType>INDIVIDUAL_CUSTOMER_GENERAL_INFORMATION_201205</customerViewType> 
        <preferences>
          <individualGeneralInformationPreferences xmlns="http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/common/2012/05/">
            <includeMinorIndicator xmlns="">true</includeMinorIndicator> 
          </individualGeneralInformationPreferences>
        </preferences>
        </customerView>
      </customerViewList>
    </getCustomerInformation>
  </soapenv:Body>
</soapenv:Envelope>

我正在尝试使用VBScript中的相对XPath访问getCustomerInformation标记。

XMLDataFile = "C:\testReqfile.xml"
Set xmlDoc = XMLUtil.CreateXML()
xmlDoc.LoadFile(XMLDataFile)
Print xmlDoc.ToString
'xmlDoc.AddNamespace "ns","xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/"
Set childrenObj = xmlDoc.ChildElementsByPath("//*[contains(@xmlns,'getCustomerInformation')]")
msgbox childrenObj.Count

但是没有返回节点。

2 个答案:

答案 0 :(得分:1)

您的XPath表达式不起作用,因为{/ 1}}与

一样
xmlns

默认命名空间,而不是属性。因此,无法使用<getCustomerInformation xmlns="http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/getCustomerInformation/2012/05/"> 访问它。

但似乎你根本不必依赖命名空间,因为元素名称(&#34; getCustomer Information&#34;)已经在说明了。要绕过名称空间中这些元素的问题,请使用@xmlns按名称选择元素。

local-name()

答案 1 :(得分:0)

正如@Mathias Müller已经在他的回答中解释的那样,xmlns定义了一个名称空间,因此不能像常规属性那样被访问。我没有XmlUtil的经验,但在标准的VBScript中你可以选择这样的节点:

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.async = False
xml.load "C:\path\to\your.xml"

If xml.ParseError Then
  WScript.Echo xml.ParseError.Reason
  WScript.Quit 1
End If

'define a namespace alias "ns"
uri = "http://service.wellsfargo.com/provider/ecpr/customerProfile/inquiry/getCustomerInformation/2012/05/"
xml.setProperty "SelectionNamespaces", "xmlns:ns='" & uri & "'"

'select nodes using the namespace alias
Set nodes = xml.SelectNodes("//ns:getCustomerInformation")