Change the order of an XML tag via T-SQL

Asked

Viewed 65 times

0

Hello!

I’m on a demand where for a certain reason, XML was generated with the order of the exchanged tags... :(

Scenario: I have a column in the SQL Server database (as of 2008) that contains NF-e XML saved from a Windows application. These XML, as I mentioned earlier, were inserted with the order of the incorrect tags. Due to the number of incorrect records already entered in the database, I thought of creating a script that changes the order of the tags as follows:

<tag>
   <tag2> Y </tag2>
   <tag1> Z </tag1>
</tag>

where the correct would be:

<tag>
   <tag1> Z </tag1>
   <tag2> Y </tag2>
</tag>

Follows partial XML model, exactly at the point I need help (first qrCode and then urlChave).

<[...]>
   <infNFeSupl> 
      <urlChave>https://www.sefaz.rs.gov.br/NFCE/NFCE-COM.aspx</urlChave> 
      <qrCode><![CDATA[https://www.sefaz.rs.gov.br/NFCE/NFCE-COM.aspx?chNFe=43180803167598000104650010000003051845389930&nVersao=100&tpAmb=1&dhEmi=323031382d30382d30335430393a32363a33372d30333a3030&vNF=55.00&vICMS=0.00&digVal=6a467a6a633157556578702f30517536627a2f732f7271306933773d&cIdToken=000001&cHashQRCode=afc69d3001a635df776512e797ea3054d00054cf]]></qrCode>
    </infNFeSupl>
</[...]>

Here is my question: How would I make this change?

I found some examples of how to modify XML attributes, but not the tags themselves. I accept suggestions.

Thanks in advance. :)

  • What is the need to reverse the order? As there are tags that identify the content, the order does not matter. If in the database it was inverted, then it seems to me that the fault lies in the import program...

  • Hello Jose, the third party API in which you receive this XML validates each tag. This change is absolutely necessary.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.