You are viewing limited content. For full access, please sign in.

Question

Question

Help to extract data from complex XML with XPath Expression

asked on September 21, 2014 Show version history

Hi

We´v XML files from NFe (Brazil Invoice Pattern) and I try capture the data from it.

I have some trouble to obtain it.

The attached file is a xml example file. To understand my problem, I extract and format 2 segments from the  xml:

<emit>
    <CNPJ>03274615000102</CNPJ>
    <xNome>ARQDIGITAL LTDA</xNome>
    <xFant>ARQDIGITAL LTDA</xFant>
    <enderEmit>
        <xLgr>SRTVS QD 701 BLOCO "O" SALA 409 ED. MULT</xLgr>
        <nro>S/N</nro>
        <xCpl>ED. MULTIEMPRES</xCpl>
        <xBairro>ASA SUL</xBairro>
        <cMun>5300108</cMun>
        <xMun>BRASILIA</xMun>
        <UF>DF</UF>
        <CEP>70340000</CEP>
        <cPais>1058</cPais>
        <xPais>BRASIL</xPais>
        <fone>6130304200</fone>
    </enderEmit>
    <IE>0739878300104</IE>
    <IM>000739878300104</IM>
    <CNAE>6203100</CNAE>
    <CRT>3</CRT>
</emit>

<dest>
    <CNPJ>10772401000113</CNPJ>
    <xNome>COOPERATIVA DE CREDITO DE LIVRE ADMISSAO DE ASSOCIADOS DO NO</xNome>
    <enderDest>
        <xLgr>AV. PRESIDENTE VARGA, No 160</xLgr>
        <nro>S/N</nro>
        <xBairro>ANGELIM</xBairro>
        <cMun>1505502</cMun>
        <xMun>PARAGOMINAS</xMun>
        <UF>PA</UF>
        <CEP>68625130</CEP>
        <cPais>1058</cPais>
        <xPais>BRASIL</xPais>
        <fone>9137295221</fone>
    </enderDest>
    <IE>ISENTO</IE>
    <email>andreia_capelari@sicredi.com.br</email>
</dest>

In the example above, the CNPJ and xNome   are present in nodes <emit> and <dest>.

Can anyone help me to do a XPath syntax to obtain this data? or give me some tips 

Best Regards 

yes

0 0

Replies

replied on September 22, 2014

See this post. Your XML uses namespaces, which the Read XML activity does not handle really well. So you need to use the //*[name()  syntax to tell it to ignore the namespace.

(I can't test your sample XML because it's incomplete, so you may need to tweak the syntax a bit more)

0 0
replied on September 22, 2014

Miruna, the attached file (NFe-35...*.txt) is a full XML.

You can rename it to ..xml.

0 0
replied on September 22, 2014

Hi Miruna ... I use the "//*[name() = 'CNPJ']"  expression to retrieve values and  in the multiple matches drop-down option, I choose the third option in the drop down - All items match (as a token of multiple value)

The results is 2 lines with different values, like this:

  • 03274615000102
  • 01245784000115

The firs value is reference  to <emit> <CNPJ>]

The second value is referenced to <dest>CNPJ>.

Well, my question is:

Can I use this values to populate different fields? 

The first value go to E.CNPJ field,  and 

The second value will populate the D.CNPJ 

If yes, can you tell me how?

Best regards

0 0
replied on September 23, 2014

You might want to ensure you are getting those values properly and not just assume one is after the other. To do that, make two tokens, with the following syntax:

//dest//CNPJ

//emit//CNPJ

Line 1 for Dest, Line 3 for Emit

0 0
replied on September 22, 2014

If I try the following XPath Expressions without having both emit and dest in the test token area, I get the proper results:

/emit/CNPJ

/emit/xNome

It would appear something goes wrong when it has both, saying:

 

In reviewing the XML you gave, it is on the same line that <dest> appears that this error for the XML is reported inside of the workflow designer activity. While I do not have a ton of experience, you can use W3Schools for a guideline on building XPath Expressions, but it would appear as though the way in which your XML is generated/formatted is improper and using two root elements. If I instead move line 22 (</emit>) to the end of the original XML, I get a value returned to me properly.

0 0
replied on September 22, 2014

\hi Kenneth, the file NFe-35140842274696002561550070006367021201408133.txt is a complete XML and you should rename it to *.xml to see a full xml file.

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.