Community
    • Login

    Changing the closing tag on a XML file, so it matches the opening tag, with notepad++ -

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    3 Posts 2 Posters 643 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • nrh_10_2022N
      nrh_10_2022
      last edited by

      I have a issue, i have a big XML file, and using notepad++.

      I have this XML syntax:

         <wp:postmeta-ingredients>
          <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
          <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
          </wp:postmeta>
      

      What i want, is to change the closing tag to:

          <wp:postmeta-ingredients>
          <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
          <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
          </wp:postmeta-ingredients>
      

      I have other data like this:

          <wp:postmeta>
          <wp:meta_key><![CDATA[ingredients_list_0_ingredients_1_quantity]]></wp:meta_key>
          <wp:meta_value><![CDATA[2000]]></wp:meta_value>
          </wp:postmeta>
      

      That i want to keep the same. So its just when the opening tag is starting with:

      <wp:postmeta-ingredients>
      

      How do i do this in notepad++, have to be with “replace all”

      Thanks.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @nrh_10_2022
        last edited by

        @nrh_10_2022

        Thank you for showing before and after data, in boxes, with data that should match and data that shouldn’t. It makes your question easier to understand.

        If I have

        <wp:postmeta-ingredients>
        <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
        <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
        </wp:postmeta>
        
        <wp:postmeta>
        <wp:meta_key><![CDATA[ingredients_list_0_ingredients_1_quantity]]></wp:meta_key>
        <wp:meta_value><![CDATA[2000]]></wp:meta_value>
        </wp:postmeta>
        
        <wp:postmeta-ingredients>
        <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
        <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
        </wp:postmeta>
        
        <wp:postmeta>
        <wp:meta_key><![CDATA[ingredients_list_0_ingredients_1_quantity]]></wp:meta_key>
        <wp:meta_value><![CDATA[2000]]></wp:meta_value>
        </wp:postmeta>
        
        • FIND = (?s)<wp:postmeta-ingredients>.*?\K</wp:postmeta>
        • REPLACE = </wp:postmeta-ingredients>
        • SEARCH MODE = Regular Expression
        • REPLACE ALL (because of \K, hitting REPLACE multiple times will not work)

        becomes

        <wp:postmeta-ingredients>
        <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
        <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
        </wp:postmeta-ingredients>
        
        <wp:postmeta>
        <wp:meta_key><![CDATA[ingredients_list_0_ingredients_1_quantity]]></wp:meta_key>
        <wp:meta_value><![CDATA[2000]]></wp:meta_value>
        </wp:postmeta>
        
        <wp:postmeta-ingredients>
        <wp:meta_key><![CDATA[ingredients_list_0_name]]></wp:meta_key>
        <wp:meta_value><![CDATA[Tészta]]></wp:meta_value>
        </wp:postmeta-ingredients>
        
        <wp:postmeta>
        <wp:meta_key><![CDATA[ingredients_list_0_ingredients_1_quantity]]></wp:meta_key>
        <wp:meta_value><![CDATA[2000]]></wp:meta_value>
        </wp:postmeta>
        

        The regex is looking for <wp:postmeta-ingredients> followed by any character(s) before the first </wp:postmeta> it reaches; then it resets (so all the stuff that’s matched so far will not be replaced), then matches </wp:postmeta>, and replaces just that last part of the match with </wp:postmeta-ingredients>.

        Barring unforeseen on edge cases, I think this should work for you.

        1 Reply Last reply Reply Quote 4
        • nrh_10_2022N
          nrh_10_2022
          last edited by

          @peterjones said in Changing the closing tag on a XML file, so it matches the opening tag, with notepad++ -:

          FIND = (?s)wp:postmeta-ingredients.*?\K</wp:postmeta>
          REPLACE = </wp:postmeta-ingredients>
          SEARCH MODE = Regular Expression
          REPLACE ALL (because of \K, hitting REPLACE multiple times will not work)

          Ehh, wow, fast and correct answer, worked like a charm.
          Super impress of the quality here.

          Thank you so much Peter :)

          1 Reply Last reply Reply Quote 5
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors