Find and replace specific word between more tags



  • Hello,
    Im using notepad ++ and i dont know how to do find+replace operation

    I have this kind of string

    <Item UPC=“1234”><Pictures><Picture Resolution=“1024” ID=“1”>somepicture.gif</Picture></Pictures><IsVisible>True</IsVisible></Item>

    And i want to change <IsVisible>True</IsVisible> to <IsVisible>False</IsVisible>

    Can you please help me? How can i do this?

    Thanks a lot!



  • @plidos said in Find and replace specific word between more tags:

    Sorry correct string is

    <Item UPC=“1234”><Pictures><Picture Resolution=“1024” ID=“1”>somepicture.gif</Picture><moretags>…</moretags></Pictures><IsVisible>True</IsVisible><moretags>…</moretags></Item>

    Thanks!



  • @plidos said in Find and replace specific word between more tags:

    <IsVisible>True</IsVisible> to <IsVisible>False</IsVisible>

    The obvious answer to your question would be to use
    find what: <IsVisible>True</IsVisible>
    replace with: <IsVisible>False</IsVisible>



  • @Ekopalypse
    yes this is simple but in that file i have over maybe 200 tags starting with <Item UPC=“(random numbers)”> so i need to change ```
    <IsVisible>True</IsVisible>

    <Item UPC=“1234”>. It is identified by <Item UPC=“1234”>
    
    So for example i need to change <IsVisible>True</IsVisible> to <IsVisible>False</IsVisible> only for tag <Item UPC="1234"> (not for 1235)
    

    <Item UPC=“1234”><Pictures><Picture Resolution=“1024” ID=“1”>somepicture.gif</Picture><moretags>…</moretags></Pictures><IsVisible>True</IsVisible><moretags>…</moretags></Item>

    
    
    Thanks


  • @plidos

    Probably the most simplistic approach would do it:

    Search for (<Item UPC=“1234”>.*?<IsVisible>)False(</IsVisible>.*?</Item>)
    and replace with \1True\2
    using Regular Expression search mode.

    There are more complicated ways which are more rigorous, but perhaps we don’t need to go there…



  • @plidos

    You could install the XML Tools plugin and use its XSLT Transformation feature.

    1. Open Plugins Admin and install XML Tools plugin.
    2. After Notepad++ has been restarted paste the XSLT code from below to an empty tab and save it for example as ChangeVisible.xsl.
    3. Open the XML file you want to process.
    4. Go to (menu) Plugins -> XML Tools -> XSLT Transformation.
    5. In the dialog popping up click on the ellipsis button and in the file selector dialog popping up select the XSL file you created in step 2.
    6. In the options field of the dialog type UPCid='1234'.
    7. Click the Transform button.
    8. A new document will be opened with your changes applied. Save it to the original file.

    To change the value of the IsVisible node of more than one Item nodes, repeat steps 6 to 8.

    The XSLT code. Please note: In line 19 (<xsl:template match="/RootNode/Item/IsVisible">) you have to provide the complete path to the IsVisible XML node, starting from the document’s root node.

    <?xml version="1.0" encoding="UTF-8"?>
    
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:output
        method="xml"
        omit-xml-declaration="no"
        indent="yes"
      />
    
      <xsl:param name="UPCid"/>
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/RootNode/Item/IsVisible">
        <xsl:choose>
          <xsl:when test="../@UPC=$UPCid">
            <xsl:copy>
              <xsl:copy-of select="@*"/>
              <xsl:text>False</xsl:text>
            </xsl:copy>
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy>
              <xsl:copy-of select="@*|node()"/>
            </xsl:copy>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    </xsl:transform>
    

Log in to reply