0

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.

Community
  • 1
  • 1
  • Please, see [here](https://stackoverflow.com/questions/51887820/obtain-attribute-names-from-xml-using-vba/51919182#51919182) if it helps... – FaneDuru Jul 15 '22 at 11:40
  • Thank you. It helped polish some of the info i'm lacking, but i don't think is relevant to my question. I already built a recursive function to iterate through the xml file, but i want to get the branch value directly. I realized my code was almost correct. What i did was to replace the root name with the "//" - xpath. – Adelina Andreea trandafir Jul 15 '22 at 12:16
  • But this was the idea... I know @T.M. and its thorough way of explaining **why** doing it in a specific way... :) – FaneDuru Jul 15 '22 at 12:22

1 Answers1

1

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.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Wow. Yes. That was exactly what i was looking for... And the reason it didn't work if i used the complete path of the branch... I am so surprised as to how many things can be done with xml from vba... Thank you :) – Adelina Andreea trandafir Jul 16 '22 at 08:27