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.

3
  • Please, see here if it helps... Commented Jul 15, 2022 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. Commented Jul 15, 2022 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... :) Commented Jul 15, 2022 at 12:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.