Home > Software design >  SelectSingleNode with each node having different namespace
SelectSingleNode with each node having different namespace

Time:07-16

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>

What i want to do is get the value of "IdentificationCode" in the branch Invoice\AccountingSupplierParty\Party\PostalAddress\Country\IdentificationCode

What i have 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. So i'm thinking i'm doing something wrong, but i don't know what since i'm a noob in XML stuff...

So, please, can you point me in the right direction?

CodePudding user response:

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.

  • Related