Search code examples
notepad++

How to select and replace XML using Notepad Replace function with condition


I work with XML which has millions of lines.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ROOT>
<trade idPosition="E56F58837T6827J1173K927479">
  <currency>BTCUSD</currency>
  <lotUSD>100000</lotUSD>
  <typeOfContract>Futures</typeOfContract>
  <isActive>1</isActive>
</trade>
<trade idPosition="S28L16487Q9127J7592O836105">
  <currency>ETHUSD</currency>
  <lotUSD>1000</lotUSD>
  <typeOfContract>CFD</typeOfContract>
  <isActive>1</isActive>
</trade>
<trade idPosition="B11F00000Z1547A1461H167729">
  <currency>SOLUSD</currency>
  <lotUSD>1000</lotUSD>
  <typeOfContract>Futures</typeOfContract>
  <isActive>1</isActive>
</trade>
</ROOT>

I want to substitute all nodes of <trade> with void ("") except nodes that have typeOfContract with CFD value to get that:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ROOT>
<trade idPosition="S28L16487Q9127J7592O836105">
  <currency>ETHUSD</currency>
  <lotUSD>1000</lotUSD>
  <typeOfContract>CFD</typeOfContract>
  <isActive>1</isActive>
</trade>
</ROOT>

I tried <trade guid=".*?">(?:(?!</?trade>).)*>CFD<.*?</trade> but if I put that in the search box and I put "" in the replace box, it doesn't work and deletes too many trading positions...


Solution

  • When using the plugin XMLTools it is easy to verify the validity of your XML, using "Check XML syntax now" from the plugin menu.

    I corrected it, and it looks like:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <ROOT>
    <trade idPosition="E56F58837T6827J1173K927479">
      <currency>BTCUSD</currency>
      <lotUSD>100000</lotUSD>
      <typeOfContract>Futures</typeOfContract>
    </trade>
    <trade idPosition="S28L16487Q9127J7592O836105">
      <currency>ETHUSD</currency>
      <lotUSD>1000</lotUSD>
      <typeOfContract>CFD</typeOfContract>
    </trade>
    <trade idPosition="B11F00000Z1547A1461H167729">
      <currency>SOLUSD</currency>
      <lotUSD>1000</lotUSD>
      <typeOfContract>Futures</typeOfContract>
    </trade>
    </ROOT>
    

    Another function of this XML Tools is: "XSL Transformation"

    When creating a file name "trades.xsl" with the following contents:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output omit-xml-declaration="yes" indent="no"/>
      <xsl:template match="/">
        <xsl:element name="ROOT">
          <xsl:for-each select="//trade[typeOfContract='CFD']">
            <xsl:copy-of select="."/>
          </xsl:for-each>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    You can transform your XML enter image description here

    A new document is created with this content:

    <ROOT><trade idPosition="S28L16487Q9127J7592O836105">
      <currency>ETHUSD</currency>
      <lotUSD>1000</lotUSD>
      <typeOfContract>CFD</typeOfContract>
    </trade></ROOT>
    

    Info on writing an XSL is pretty hard. (IMHO), that's why I used XmlStarlet which van create XSL stylesheets.

    xml sel -C -t -e root -m //trade[typeOfContract='CFD'] -c . trade.xml > trade.xsl