Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    XML File - Create an element and get a value from existing attribute

    Help wanted · · · – – – · · ·
    3
    10
    133
    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.
    • Software Support
      Software Support last edited by

      Hello everyone!!

      I have an xml file and I am trying to create a new element and copy a value of an existing attribute on that.
      So, I would like to:

      1. Create a new element <weight> (above Price).
      2. Put the value of the existing “weight” label as value on the new <weight> element.

      To be more specific I quote an example of what I need to do…

      -------BEFORE----------
      <product>
      <code>009-110360</code>
      <Name>Test Prodcut 1</Name>
      <description>Desc of Product 1</description>
      <price>200.00</price>
      <specs>
      <spec>
      <label>Color</label>
      <value>Blue</value>
      </spec>
      <spec>
      <label>Watt</label>
      <value>30</value>
      </spec>
      <spec>
      <label>Weight</label>
      <value>2</value>
      </spec>
      <spec>
      <label>Packet</label>
      <value>5</value>
      </spec>
      </specs>
      </product>

      -------AFTER----------
      <product>
      <code>009-110360</code>
      <Name>Test Prodcut 1</Name>
      <description>Desc of Product 1</description>
      <price>200.00</price>
      <Weight>2</Weight>
      <specs>
      <spec>
      <label>Color</label>
      <value>Blue</value>
      </spec>
      <spec>
      <label>Watt</label>
      <value>30</value>
      </spec>
      <spec>
      <label>Weight</label>
      <value>2</value>
      </spec>
      <spec>
      <label>Packet</label>
      <value>5</value>
      </spec>
      </specs>
      </product>

      after.png

      Thank you all in advance!
      Chris

      Alan Kilborn 1 Reply Last reply Reply Quote 0
      • Alan Kilborn
        Alan Kilborn @Software Support last edited by

        @Software-Support

        It would have been better if you had followed the advice HERE for posting your question, because that way your sample data would be more representative of your real data. I know that these are different things because you posted data AND a screenshot of that data as it appears to you in N++. I’m speaking specifically of all of the leading whitespace shown in the screenshot but not reflected in your sample data.

        Software Support 1 Reply Last reply Reply Quote 0
        • Alan Kilborn
          Alan Kilborn last edited by

          Even with your data represenation problems, I think something like this could work:

          Find: (?s-i)^(<product>.*?(\h+)<price>.*?</price>(\R))(.*?Weight.*?(\d+).*?</product>)
          Replace: ${1}${2}<Weight>${5}</Weight>${3}${4}
          Mode: Regular expression

          More info on regex is HERE.

          Software Support 1 Reply Last reply Reply Quote 1
          • Software Support
            Software Support @Alan Kilborn last edited by

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • Software Support
              Software Support last edited by

              I apologize for that. You’re right about my posting.

              Your solution solved my question!!!
              Thank you very very much!!!

              Alan Kilborn 1 Reply Last reply Reply Quote 0
              • guy038
                guy038 last edited by guy038

                Hello, @software-support, @alan-kilborn and All,

                Oh ! Alan beat me at it :-)

                Two possible solutions :

                • SEARCH (?s)<product>.+?</price>\R(?!\h*<Weight>)\K(?=(?:(?!</product>).)+?Weight.+?(\d+))

                • REPLACE <Weight>\1</Weight>\r\n   OR   <Weight>\1</Weight>\n if you use UNIX files

                • Check the Regular expression search mode

                • Click once on the Replace All button ( Do not use the Replace button !)

                OR

                • SEARCH (?s)(<product>.+?</price>\R)(?!\h*<Weight>)(?=(?:(?!</product>).)+?Weight.+?(\d+))

                • REPLACE \1<Weight>\2</Weight>\r\n   OR   \1<Weight>\2</Weight>\n if you use UNIX files

                • Check the Regular expression search mode

                • Click once on the Replace All button or several times on the Replace buton


                For instance, with the INPUT text :

                <product>
                <code>009-110360</code>
                <Name>Test Prodcut 1</Name>
                <description>Desc of Product 1</description>
                <price>200.00</price>
                <spec>
                <label>Color</label>
                <value>Blue</value>
                </spec>
                <spec>
                <label>Watt</label>
                <value>30</value>
                </spec>
                <spec>
                <label>Weight</label>
                <value>2</value>
                </spec>
                <spec>
                <label>Packet</label>
                <value>5</value>
                </spec>
                </specs>
                </product>
                <product>
                <code>009-110360</code>
                <Name>Test Prodcut 1</Name>
                <description>Desc of Product 1</description>
                <price>200.00</price>
                <spec>
                <label>Color</label>
                <value>Blue</value>
                </spec>
                <spec>
                <label>Watt</label>
                <value>30</value>
                </spec>
                <spec>
                <label>Weight</label>
                <value>279</value>
                </spec>
                <spec>
                <label>Packet</label>
                <value>5</value>
                </spec>
                </specs>
                </product>
                

                you’ll get the expected OUTPUT text :

                <product>
                <code>009-110360</code>
                <Name>Test Prodcut 1</Name>
                <description>Desc of Product 1</description>
                <price>200.00</price>
                <Weight>2</Weight>
                <spec>
                <label>Color</label>
                <value>Blue</value>
                </spec>
                <spec>
                <label>Watt</label>
                <value>30</value>
                </spec>
                <spec>
                <label>Weight</label>
                <value>2</value>
                </spec>
                <spec>
                <label>Packet</label>
                <value>5</value>
                </spec>
                </specs>
                </product>
                <product>
                <code>009-110360</code>
                <Name>Test Prodcut 1</Name>
                <description>Desc of Product 1</description>
                <price>200.00</price>
                <Weight>279</Weight>
                <spec>
                <label>Color</label>
                <value>Blue</value>
                </spec>
                <spec>
                <label>Watt</label>
                <value>30</value>
                </spec>
                <spec>
                <label>Weight</label>
                <value>279</value>
                </spec>
                <spec>
                <label>Packet</label>
                <value>5</value>
                </spec>
                </specs>
                </product>
                

                In addition, there’s a security as you cannot run the regex S/R twice in order to get the wrong OUTPUT :

                ...
                ...
                <description>Desc of Product 1</description>
                <price>200.00</price>
                <Weight>2</Weight>
                <Weight>2</Weight>
                <spec>
                ...
                ...
                

                Best Regards,

                guy038

                Alan Kilborn 1 Reply Last reply Reply Quote 1
                • Alan Kilborn
                  Alan Kilborn @Software Support last edited by

                  @Software-Support said in XML File - Create an element and get a value from existing attribute:

                  Your solution solved my question!!!

                  That’s good, but also, hopefully you decided to learn more about HOW the solution works, for the NEXT time you need such a thing.

                  1 Reply Last reply Reply Quote 1
                  • Alan Kilborn
                    Alan Kilborn @guy038 last edited by

                    @guy038 said:

                    Two possible solutions

                    Actually, there are MANY possible solutions, and some “protect” your data more than others by enforcing validation of conditions. It’s all about what you need in a given situation.

                    When we respond to help requests here, we typically provide exactly what is requested, which MAY not meet the true need.

                    1 Reply Last reply Reply Quote 2
                    • Software Support
                      Software Support @Alan Kilborn last edited by

                      @Alan-Kilborn

                      Hello Alan.
                      As I mentioned before, your way worked perfect and it does what I wished!
                      But, as I can see, decimal numbers are rounded and they are appeared as integers.
                      For example: The value 2.92 will be appeared as 2, the value 3.1 as 3 etc…

                      How could it be fixed ?
                      Thanx in advance!!

                      Alan Kilborn 1 Reply Last reply Reply Quote 0
                      • Alan Kilborn
                        Alan Kilborn @Software Support last edited by

                        @Software-Support said in XML File - Create an element and get a value from existing attribute:

                        The value 2.92 will be appeared as 2

                        Well, for exactly that situation, you’d want to change the \d+ appearing in the original Find expression to \d+\.\d+, I guess…

                        But this gets into a bigger topic of how to match floating point numbers as well as integers.

                        And that really isn’t a Notepad++ topic, so patience with such questions wears thin. Suggest you follow the link I provided before about regular expressions.

                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post
                        Copyright © 2014 NodeBB Forums | Contributors