xmlvbaselectsinglenode

SelectSingleNode with each node having different namespace


I have the following XML:

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:CustomizationID xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">urn:cen.eu:en16931:2017#compliant#urn:efactura.mfinante.ro:CIUS-RO:1.0.0</cbc:CustomizationID>
<cac:AccountingSupplierParty xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
  <cac:Party>
    <cac:PostalAddress>
       <cbc:StreetName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">xxxxxx 8</cbc:StreetName>
       <cbc:CityName xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Bacau</cbc:CityName>
       <cbc:PostalZone xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">600093</cbc:PostalZone>
       <cbc:CountrySubentity xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">RO-BC</cbc:CountrySubentity>
       <cac:Country>
          <cbc:IdentificationCode xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">RO</cbc:IdentificationCode>

I want the value of "IdentificationCode" in the branch Invoice\AccountingSupplierParty\Party\PostalAddress\Country\IdentificationCode

I tried:

Dim dom As New MSXML2.DOMDocument60
Dim nod As IXMLDOMNode
Dim branch As String
Dim Ns As String

Ns = "xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'"

With dom
    .SetProperty "SelectionNamespaces", Ns
    .async = False
    .Load xFile
End With

'here was the answear: "/Invoice" TO "//"
branch = "//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode"

Set nod = dom.selectSingleNode(branch)

If Not nod Is Nothing Then Stop
 
Set dom = Nothing

The problem is nod is always nothing.


Solution

  • FYI in the future if you have a "default” namespace like this (notice there's no alias on this one)

    <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
    

    ...then you can create a dummy alias while including it in your namespaces list:

     Ns = "xmlns:xxx='urn:oasis:names:specification:ubl:schema:xsd:Invoice-2' " & _
          "xmlns:cbc='urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2' " & _
          "xmlns:cac='urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'"
    

    and then include the dummy alias with Invoice:

    branch = "/xxx:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode"
    

    That works for me using your XML.