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

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:

![image|665x499](upload://rdEIzDZODR8g775wiWtYWGrSIsM.png)

<?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>

I want to extract elements with namespace “cac”, but I want detect with something functions to xDoc or something like that to get the value: “urn:oasis…”

xmlns:cac=“urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2”

for name space handling have a look here:
XML Extraction - Handling Namespaces (Tableau, Soap, OASIS types) - Help / Community - UiPath Community Forum

for the document default namespace we can do:
grafik

For collecting all other namespace definitions we can do:

xDoc.Root.Attributes.where(Function (x) x.IsNamespaceDeclaration).Select(Function (x) x.Value).tolist

Creating some helpers e.g a dictionary with a key for the prefix and value as XNamespace we can do:


The document Default Namespace does not have a local name and we use the key xmlns for:

xDoc.Root.Attributes.where(Function (x) x.IsNamespaceDeclaration).ToDictionary( Function (x) x.Name.LocalName, Function (x) x.value)

Similiar we can also do on other XElements, when namespaces are defined there

Thanks @ppr and since i could read the xml and then extract from a namespace i specify its fields and values please, I am doing the following:

  1. read xml text
  2. deserialize xml
  3. xmlDes.Descendants(xmlns+“Invoice”) to loop only elements within the invoice.

xmlns is a variable with the value extrated from xml.

if possible share the complete XML as textfile

Your queastion is unclear. We can avoid some code parts when working with xDoc.Root

xDoc.Root.Descendants(…

Also have a look here for getting all CBC elments when direct next level under root:


can be used for a loop within for each

For a prototype (assuming some contstraints) we can create a Lookup Dictionary like:

Yes, I want to extract elements from element in a datatable. I have 2 items InvoiceLine

Find some starter for your modellings. Prototyping done with log messages:
Variables:

Preperation:
grafik

Creating a dict for Namespace lookups:
grafik

attr in xDoc.Root.Attributes.where(Function (x) x.IsNamespaceDeclaration)
dictNameSpaces(attr.Name.LocalName) = tmpXNS

Value extraction:

item in xDoc.Root.Descendants(dictNameSpaces("cac") + "InvoiceLine")
item.Descendants(dictNameSpaces("cbc") + "LineExtensionAmount").First().Value
item.Descendants(dictNameSpaces("cbc") + "Percent").First().Value
item.Descendants(dictNameSpaces("cbc") + "ItemClassificationCode").First().Value
1 Like

thanks so much @ppr You are fantastic!

@odelacruz
scoping this topic to the namespace retrieval and an initial retrieval case we do feel that the question is answered and the topic can be closed. May we ask you to do this? Thanks:
Forum FAQ - How to mark a post as a solution - News / Tutorials - UiPath Community Forum

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.