1

我们从 BACS Clearing 收到以下格式的 XML 数据:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Oracle Reports version 10.1.2.3.0 -->
<?xml-stylesheet href="file:///o:/Dev/Development Projects 2014/DP Team Utilities/D-02294 DDI Voucher XML Conversion Tool/DDIVoucherStylesheet.xsl" type="text/xsl" ?>
<VocaDocument xmlns="http://www.voca.com/schemas/messaging" xmlns:msg="http://www.voca.com/schemas/messaging" xmlns:cmn="http://www.voca.com/schemas/common" xmlns:iso="http://www.voca.com/schemas/common/iso" xmlns:env="http://www.voca.com/schemas/envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.voca.com/schemas/messaging http://www.voca.com/schemas/messaging/Voca_AUDDIS_AdviceofDDI_v1.0.xsd">
  <Data>
    <Document type="AdviceOfDDIReport" created="2014-08-19T00:59:15" schemaVersion="1.0">
      <StreamStart>
        <Stream>
          <AgencyBankParameter>234</AgencyBankParameter>
          <BankName>LLOYDS BANK PLC</BankName>
          <BankCode>9876</BankCode>
          <AgencyBankName>BANK OF CYPRUS UK LTD</AgencyBankName>
          <AgencyBankCode>5432</AgencyBankCode>
          <StreamCode>01</StreamCode>
          <VoucherSortCode>SC998877</VoucherSortCode>
          <VoucherAccountNumber>12348765</VoucherAccountNumber>
        </Stream>
      </StreamStart>
      <DDIVouchers>
        <Voucher>
          <TransactionCode> NEW</TransactionCode>
          <OriginatorIdentification><ServiceUserName>A SERVICE NAME </ServiceUserName><ServiceUserNumber>223344</ServiceUserNumber></OriginatorIdentification>
          <PayingBankAccount><BankName>A SMALL BANK UK LTD</BankName><AccountName>AN INDIVIDUAL  </AccountName><AccountNumber>77553311</AccountNumber><UkSortCode>SC776655</UkSortCode></PayingBankAccount>
          <ReferenceNumber>BACS001122       </ReferenceNumber>
          <ContactDetails><PhoneNumber>021 223344</PhoneNumber><FaxNumber> </FaxNumber><Address><cmn:AddresseeName>a name</cmn:AddresseeName><cmn:PostalName>a place</cmn:PostalName><cmn:AddressLine>an address</cmn:AddressLine><cmn:TownName>A Town</cmn:TownName><cmn:CountyIdentification> </cmn:CountyIdentification><cmn:CountryName>UNITED KINGDOM</cmn:CountryName><cmn:ZipCode>AA1 2BB</cmn:ZipCode></Address></ContactDetails>
          <ProcessingDate>2014-08-19</ProcessingDate>
          <BankAccount><FirstLastVoucherCode>FirstLast</FirstLastVoucherCode><AgencyBankCode>7890</AgencyBankCode><SortCode>SC223344</SortCode><AccountNumber>99886655</AccountNumber><TotalVouchers>1</TotalVouchers></BankAccount>
        </Voucher>
        <Voucher>
...

当我将 xml 加载到 XPathVisualizer 工具中时,它可以很好地使用这样的 XPath 表达式:

VocaDocument/Data/Document/DDIVouchers/Voucher

但是,当我在 MS Excel 的 VBA 中使用相同的xpath 将值检索到工作表中时,它不起作用。

这是我在 MS Execl VBA 中使用的代码:

    Dim nodeList As IXMLDOMNodeList
    Dim nodeRow As IXMLDOMNode
    Dim nodeCell As IXMLDOMNode

    Dim rowCount As Integer
    Dim cellCount As Integer
    Dim rowRange As Range
    Dim cellRange As Range
    Dim sheet As Worksheet
    Dim dom As DOMDocument60

    Dim xpathToExtractRow As String
    xpathToExtractRow = "VocaDocument/Data/Document/DDIVouchers/Voucher"

    ' OTHER XPath examples
    ' xpathToExtractRow = "VocaDocument/Data/Document/StreamStart/Stream/BankName"
    ' xpathToExtractRow = "VocaDocument/Data/Document/DDIVouchers/Voucher/ContactDetails/Address/cmn:AddresseeName" ' NOTICE cmn namespace!
    ' xpathToExtractRow = "VocaDocument/Data/Document/DDIVouchers/Voucher/ProcessingDate

    Set domIn = New DOMDocument60
    domIn.setProperty "SelectionLanguage", "XPath"

    domIn.load (Application.GetOpenFilename("XML Files (*.xml), *.xml", , "Please select the xml file"))
    Set sheet = ActiveSheet
    Set nodeList = domIn.DocumentElement.SelectNodes(xpathToExtractRow)
    Set nodeRow = domIn.DocumentElement.SelectSingleNode(xpathToExtractRow) '"/*/Data//StreamStart/Stream/*").nodeName

    rowCount = 0
    Workbooks.Add
    For Each nodeRow In nodeList
        rowCount = rowCount + 1
        cellCount = 0
        For Each nodeCell In nodeRow.ChildNodes
            cellCount = cellCount + 1
            Set cellRange = sheet.Cells(rowCount, cellCount)
            cellRange.Value = nodeCell.Text
        Next nodeCell
    Next nodeRow

    End Sub

那么我缺少什么,我需要将命名空间添加到 DOM 对象或其他东西?如果是这样,我应该使用谁添加所有命名空间xmlDoc.setProperty("SelectionNamespaces",

谢谢

4

1 回答 1

4

您需要注册默认命名空间:

xmlDoc.setProperty "SelectionNamespaces", "xmlns:ns='http://www.voca.com/schemas/messaging'"

然后,您需要在声明默认命名空间的范围内的所有节点的开头使用已注册的命名空间前缀:

ns:VocaDocument/ns:Data/ns:Document/ns:DDIVouchers/ns:Voucher

这是因为后代节点自动从祖先继承默认命名空间,除非在后代级别声明了不同的默认命名空间,或者使用了指向不同命名空间的前缀。

于 2014-09-16T13:33:46.017 回答