I anticipate that at some point I’ll have a problem with regex (later)
Below is the complete XML structure (downloaded from the website that explains the rules).
Explanation
Notice that some tags are repeated, for example, the group:
<identificacao>
<CPF>...</CPF>
<CNPJ>...</CNPJ>
<IE>...</IE>
</identificacao>
This group appears twice; however, the “parent” tag is different:
(1)
<contribuinteEmitente>
<identificacao>
<CPF>...</CPF>
<CNPJ>...</CNPJ>
<IE>...</IE>
</identificacao>
(2)
<contribuinteDestinatario>
<identificacao>
<CPF>...</CPF>
<CNPJ>...</CNPJ>
<IE>...</IE>
There are several rules in the XMLs (which I won’t explain here), but depending on the condition, some of them will be omitted. For example, if there is no value for the <CNPJ>
tag of the <contribuinteEmitente>
group, it should not be included, nor should <IE>
, resulting in this:
<contribuinteEmitente>
<identificacao>
<CPF>...</CPF>
</identificacao>
I’ll figure out how to implement these rules, but there’s still one point I’m unsure about. In your help, you used a regex replace, assuming we are iterating the <CNPJ>
field of the <contribuinteEmitente>
group. When we use regexReplace to modify the XML, it will also change the same tag in the <contribuinteDestinatario>
group.
Since we are at the “row” level without any identification, it’s more complicated to identify.
These types of rules in XMLs are very common, where tags are repeated within groups. I asked Gemini to teach me how to build an XML based on conditions, and it gave me rules in Python (which I know nothing about).
Besides your help, I thought: How do large companies build XMLs in an organized way, considering these rules? My idea would be, instead of doing regex replace, to build the XML in blocks of groups based on IF rules. But for that, “I have no idea yet.”
Anyway, one step at a time. Tonight I’ll start modifying my code considering your rules.
(By the way, I’ve never used the Recursive Loop. I use the group loop a lot, and I’ve watched several videos and examples, and it never clicked in my head “when” I should use it. In your example, it became easier for me to understand. You used it to iterate over the same “XML” column until the iteration for the number of rows ended. Very interesting.)
Completed structure
Versão 2.00
<TDadosGNRE versao="2.00">
<ufFavorecida>...</ufFavorecida>
<tipoGnre>...</tipoGnre>
<contribuinteEmitente>
<identificacao>
<CPF>...</CPF>
<CNPJ>...</CNPJ>
<IE>...</IE>
</identificacao>
<razaoSocial>...</razaoSocial>
<endereco>...</endereco>
<municipio>...</municipio>
<uf>...</uf>
<cep>...</cep>
<telefone>...</telefone>
</contribuinteEmitente>
<itensGNRE>
<item>
<receita>...</receita>
<detalhamentoReceita>...</detalhamentoReceita>
<documentoOrigem tipo="..." >...</documentoOrigem>
<produto>...</produto>
<referencia>
<periodo>...</periodo>
<mes>...</mes>
<ano>...</ano>
<parcela>...</parcela>
</referencia>
<dataVencimento>...</dataVencimento>
<valor tipo="..." >...</valor>
<convenio>...</convenio>
<contribuinteDestinatario>
<identificacao>
<CPF>...</CPF>
<CNPJ>...</CNPJ>
<IE>...</IE>
</identificacao>
<razaoSocial>...</razaoSocial>
<municipio>...</municipio>
</contribuinteDestinatario>
<camposExtras>
<campoExtra>
<codigo>
<valor>
</campoExtra>
</camposExtras>
</item>
</itensGNRE>
<valorGNRE>...</valorGNRE>
<dataPagamento>...</dataPagamento>
</TDadosGNRE>