How to automatically detect a namespace in the root label, to then extract its value into a variable and be able to loop through the elements.
Example:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cacadd="urn:e-billing:aggregates" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cbcadd="urn:e-billing:basics" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:extadd="urn:e-billing:extension" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:sac="urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<cacadd:ExtraParameters xmlns:sac="urn:sunat.names.specification.ubl_2_0.2_0.peru:schema:xsd:SunatAggregateComponents-1">
<cbcadd:extra name="@@APLICATIVO"><![CDATA[05]]></cbcadd:extra>
<cbcadd:extra name="@@CONTRATO"><![CDATA[001104090100008266]]></cbcadd:extra>
<cbcadd:extra name="@@LLAVE"><![CDATA[MIRB/T428/BG4CCMC0/000000004/2346]]></cbcadd:extra>
</cacadd:ExtraParameters>
</ext:ExtensionContent>
</ext:UBLExtension>
<ext:UBLExtension>
<ext:ExtensionContent><Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="SIGN_20100130204-01-FB07-07853985"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue>3hEDhDIQWYg2zYG/9QqV8BsyMmQ=</DigestValue></Reference></SignedInfo><SignatureValue>EPy0+Tezq8lY5alOg9oN7jhwHZ6/3TaJftrfzXqf0qbVWYcoqvOHUY4jfb1U3JKAGTUQYSwllfHPWjfHzUBLP0sZfVNbXXxDsF4E04ICi4JX7YdjusANuPnUUnh/GdvjmGWxAW80NO4Cn2A8xnceLqxwfhFTtqlojFzPp5vReIj2znIXHSAsnSgS6qB2eHUpoLmw9GKh2GZfBpC33UngB9MpWI6c4kgg5eExORwZu9qsr7BBBButgKi7NwkP94OLQiHXhBZHGihCNBzpEX0UMwqgOVzviw+zG4v7fCDrkdWHmJTf/HcdnHC16pscD/9H2ktX2HdBLpLkMr0gLGqQkw==</SignatureValue><KeyInfo><X509Data><X509Certificate>rgergergerge</X509Certificate></X509Data></KeyInfo></Signature></ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID schemeAgencyName="PE:SUNAT">2.0</cbc:CustomizationID>
<cbc:ID>FB07-07853985</cbc:ID>
<cbc:IssueDate>2022-10-31</cbc:IssueDate>
<cbc:DueDate>2022-10-31</cbc:DueDate>
<cbc:InvoiceTypeCode listAgencyName="PE:SUNAT" listID="2103" listName="Tipo de Documento" listSchemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo51" listURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01" name="Tipo de Operacion">01</cbc:InvoiceTypeCode>
<cbc:Note languageLocaleID="1000"><![CDATA[CUARENTA Y CINCO Y 00/100 SOLES]]></cbc:Note>
<cbc:DocumentCurrencyCode listAgencyName="United Nations Economic Commission for Europe" listID="ISO 4217 Alpha" listName="Currency">PEN</cbc:DocumentCurrencyCode>
<cac:Signature>
<cbc:ID>SIGN_20100130204-01-FB07-07853985</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID>20557103920</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name><![CDATA[ESCONTECH DEL PERĂš]]></cbc:Name>
</cac:PartyName>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#SIGN_20100130204-01-FB07-07853985</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeAgencyName="PE:SUNAT" schemeID="6" schemeName="Documento de Identidad" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06">20100130204</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name><![CDATA[BBVA]]></cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:StreetName/>
</cac:PostalAddress>
<cac:PartyLegalEntity>
<cbc:RegistrationName><![CDATA[BANCO BBVA PERĂš]]></cbc:RegistrationName>
<cac:RegistrationAddress>
<cbc:ID schemeAgencyName="PE:INEI" schemeName="Ubigeos">150131</cbc:ID>
<cbc:AddressTypeCode>0000</cbc:AddressTypeCode>
<cbc:CitySubdivisionName><![CDATA[URB. EL PALOMAR]]></cbc:CitySubdivisionName>
<cbc:CityName><![CDATA[LIMA]]></cbc:CityName>
<cbc:CountrySubentity><![CDATA[LIMA]]></cbc:CountrySubentity>
<cbc:District><![CDATA[SAN ISIDRO]]></cbc:District>
<cac:AddressLine>
<cbc:Line><![CDATA[AV. REPUBLICA DE PANAMA NRO 3055 - LIMA 27 - SAN ISIDRO. LIMA.PERU]]></cbc:Line>
</cac:AddressLine>
<cac:Country>
<cbc:IdentificationCode listAgencyName="United Nations Economic Commission for Europe" listID="ISO 3166-1" listName="Country"><![CDATA[PE]]></cbc:IdentificationCode>
</cac:Country>
</cac:RegistrationAddress>
</cac:PartyLegalEntity>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeAgencyName="PE:SUNAT" schemeID="6" schemeName="Documento de Identidad" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06">20422488198</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:StreetName/>
</cac:PostalAddress>
<cac:PartyLegalEntity>
<cbc:RegistrationName><![CDATA[EQUINOX INTERNATIONAL SAC]]></cbc:RegistrationName>
<cac:RegistrationAddress>
<cac:AddressLine>
<cbc:Line/>
</cac:AddressLine>
</cac:RegistrationAddress>
</cac:PartyLegalEntity>
</cac:Party>
<cac:AccountingContact>
<cbc:ElectronicMail><![CDATA[JORGE.GONZALES@PE.ASEYCO.COM]]></cbc:ElectronicMail>
</cac:AccountingContact>
</cac:AccountingCustomerParty>
<cac:PaymentTerms>
<cbc:ID>FormaPago</cbc:ID>
<cbc:PaymentMeansID>Contado</cbc:PaymentMeansID>
</cac:PaymentTerms>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="PEN">0.00</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="PEN">45.00</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="PEN">0.00</cbc:TaxAmount>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:ID schemeAgencyName="PE:SUNAT" schemeName="Codigo de tributos" schemeURI="urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo05">9998</cbc:ID>
<cbc:Name>INA</cbc:Name>
<cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="PEN">45</cbc:LineExtensionAmount>
<cbc:TaxInclusiveAmount currencyID="PEN">45</cbc:TaxInclusiveAmount>
<cbc:PayableAmount currencyID="PEN">45.00</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>