Merge same parameters in XML file



  • Hi

    How I can make from this data
    <param name=“Product”>Multifunction valves</param>
    <param name=“Coolant”>R113</param>
    <param name=“Coolant”>R114</param>
    <param name=“Coolant”>R1233zd(E)</param>
    <param name=“Coolant”>R1234yf</param>
    <param name=“Coolant”>R1234ze(E)</param>
    <param name=“Coolant”>R125</param>
    <param name=“Coolant”>R1270</param>
    <param name=“Coolant”>R1336mzz(Z)</param>
    <param name=“Coolant”>R134a</param>
    <param name=“Coolant”>R152a</param>
    <param name=“Coolant”>R170</param>
    <param name=“Coolant”>R22</param>

    like this
    <param name=“Product”>Multifunction valves</param>
    <param name=“Coolant”>R113; R114; R1233zd(E); R1234yf; R1234ze(E); R125; R1270; R1336mzz(Z); R134a; R152a; R170; R22</param>



  • Hello, @petr-andreev,

    No problem with regular expressions !

    Here are the few steps to do :

    • Go back at the very beginning of your document ( Ctrl + Origin )

    • Open the Replace dialog ( Ctrl + H )

    • SEARCH (?-is)</param>\R<param name="Coolant">|^(.+Product.+?);\x20

    • REPLACE (?1\1</param>\r\n<param name="Coolant">:;\x20)

    • UNcheck the Wrap around option, if necessary

    • Select the Regular expression search mode

    • Click TWICE, on the REplace All button

    Et voilà !

    At first sight, you may think that these regexes are complicated, But I tried to give some information on them, in the Notes section, below !


    So, from your original text, below :

    <param name="Product">Multifunction valves</param>
    <param name="Coolant">R113</param>
    <param name="Coolant">R114</param>
    <param name="Coolant">R1233zd(E)</param>
    <param name="Coolant">R1234yf</param>
    <param name="Coolant">R1234ze(E)</param>
    <param name="Coolant">R125</param>
    <param name="Coolant">R1270</param>
    <param name="Coolant">R1336mzz(Z)</param>
    <param name="Coolant">R134a</param>
    <param name="Coolant">R152a</param>
    <param name="Coolant">R170</param>
    <param name="Coolant">R22</param>
    

    You get, after TWO clicks on the Replace All button, your wanted text, below ;-))

    <param name="Product">Multifunction valves</param>
    <param name="Coolant">R113; R114; R1233zd(E); R1234yf; R1234ze(E); R125; R1270; R1336mzz(Z); R134a; R152a; R170; R22</param>
    

    Notes,

    • The modifiers (?-is) forces a sensitive way of search and the special dot character will match a single standard character, only !

    • Then, due to the alternative symbol ( | ), the regex engine looks for, either :

      • Case A The string </param>, followed by End of Line character(s) and ending with the string <param name=“Coolant”> ( part </param>\R<param name="Coolant"> )

      • Case B The string, located between a beginning of line and the first semicolon ;, containing the string Product ( part ^(.+Product.+?); ), followed by a space character ( part \x20 )

    • In replacement, it rewrites, either :

      • Case A A semicolon, followed by a space character ( part ;\x20 ), which is the ELSE part of the conditional replacement, located after the colon symbol :

      • Case B The group 1 ( The string from beginning of line, till a semicolon character, excluded, containing the string Product ( part \1 ), followed by the string </param>, a line break and the string <param name=“Coolant”> ( part </param>\r\n<param name="Coolant"> )
        It is the THEN part of the conditional replacement, located between the (?1 syntax and the colon symbol :


    Remarks :

    • Case A occurs first, because there’s no semicolon symbol ( ; ), in your original text !

    • Of course, in these regexes, you may change the tags Product and Coolant, as you like to :-)

    Best Regards,

    guy038


Log in to reply