Need help to replace tags to one xml from another xml
-
@ganesan-govindarajan
I now have the regex ready in “macro” format.As I’m not sure of your capabilities I will have you create a “test” macro first. This will allow us to make a replacement in the correct shortcuts.xml file with the macro I will provide (below).
-
So open Notepad++ with a single tab (new file). If already open, close and re-open it.
-
Type a single character in this file, lets do a “1”.
-
Click on the “Macro” option in the menu at top, then click on “Start Recording”.
-
Type Ctrl-H (that’s the ctrl key with a H key), this opens the “Replace” function.
-
In the Find What field type “1”, same as we typed in step 2 above.
-
In the Replace field type “9”.
-
Click on the “Replace button”. Then click on “Close” button.
-
Click again on the Macro menu option, then on “Stop Recording”.
-
Click on Menu option again, and click on “Save current recorded macro”. Give it a name, let’s do “tagtransfer”.
-
Close the current tab, no need to save this file.
-
Click “open…”, you can do “File”, then “Open…” or the 2nd icon from left achieves the same result.
-
In the filename window paste the following text (in red).
%AppData%\Notepad++\shortcuts.xml
and click “Open”. -
Look for the words “tagtransfer” that you typed in step 9 above.
If this file does not show the macro you recorded then your Notepad++ installation is different from the normal setup. In that case you will need to open the shortcuts.xml file in either C:\Program Files (x86)\Notepad or C:\Program Files\Notepad depending on whether you installed the 32bit version of the 64bit version. Look for the macro you recorded in step 9. -
Once the correct shortcuts.xml file has been opened you will copy the macro below (in the shaded window) over top of the macro you saved in step 9 above. Take note of the start and end lines you will be copying over top of, using my macro as a guide.
-
Once you have inserted the new macro, save the shortcuts.xml file, then exit and reload Notepad++. THIS STEP VERY IMPORTANT!!
-
Under the Macro menu option you should now see the replacement macro listed, look for “SwapTag”. If it’s there then good. If not then likely you have missed a crucial step above.
<Macro name=“SwapTag” Ctrl=“no” Alt=“no” Shift=“no” Key=“0”>
<Action type=“3” message=“1700” wParam=“0” lParam=“0” sParam=“” />
<Action type=“3” message=“1601” wParam=“0” lParam=“0” sParam=“(?s)(<desc>)(?!<)(.+?)(</desc>.+={10}.+?<desc>)(?=<)” />
<Action type=“3” message=“1625” wParam=“0” lParam=“2” sParam=“” />
<Action type=“3” message=“1602” wParam=“0” lParam=“0” sParam=“\1\3\2” />
<Action type=“3” message=“1702” wParam=“0” lParam=“768” sParam=“” />
<Action type=“3” message=“1701” wParam=“0” lParam=“1609” sParam=“” />
</Macro>
Once all steps have been completed successfully you are now in a position to actually run the macro over your 2 files.
- Load both the “donor” (has the <desc>
text here
</desc>) and “patient” (has the blank <desc></desc>) files into Notepad++. - In the “donor” file you need to establish the exact number of entries, so in the “Find” function (Ctrl-F) type
(?s)<desc>.+?</desc>
, this is what I mentioned in a previous post. - Click on the “Count” button, this number needs writing down as we use it later.
- In the “patient” file, add 2 lines at the top, so make it the first and second lines, and type 10 “=” (equal) signs across the second line. This is a “delimiter”, so it allows the macro to differentiate between source data and destination data.
- Back to the “donor” file and copy all the content of this file.
- Back to the “patient” file and paste the content just copied into line 1.
- At this point you are ready to run the macro. So click on the Macro menu option, select “Run a Macro multiple times”
- In the “Macro to Run” window, select the “SwapTag” macro. In the Run window type the number you wrote down earlier.
- Click on the “Run” button.
If all steps have been done correctly you should now have all the content you wanted moved, now located in the “patient” file. At this point check the last few lines above the “======” to make sure the <desc></desc> entries are empty. Do the same with the “patient” file, checking the last few entries that content has been added. If happy with the result, you can remove the “donor” file portion and the delimiter line. Save the rest as the “patient” file.
When I tested on some dummy data with 294 entries to transfer it only took a second or so to complete the process. So most your time will be taken up with the setup process. The actual transfer of data is very quick.
The actual regex used is:
Find What:(?s)(<desc>)(?!<)(.+?)(</desc>.+={10}.+?<desc>)(?=<)
Replace with:\1\3\2
The description of the regex is:
(?s) look across lines (the . means every character)
(<desc>) look for the text <desc> and save as group 1
(?!<) what follows cannot be another tag (</desc>) as that would mean there isn’t any text to transfer.
(.+?) this grabs the text within the tag and saves as group 2
(</desc>.+={10}.+?<desc>) stops group 2 from grabbing more than we want, then it also confirms the delimiter is beyond the text we have grabbed, and also sets the cursor up to the position where we want to insert the text.
(?=<) stops the cursor at the point where there is an empty <desc></desc> tag, so where we want to insert the text.From that you can see that the macro interprets some characters and shows a code in their place. In the regex you will see the “<desc>” and “</desc>” texts, this is the "heart of the regex. Changing these texts to use other tags will allow it to work in other situations.
I hope this helps you. Please do let me/us know how it went. If issues then we want to know, maybe something that was not mentioned during the postsings.
Terry
-
-
@ganesan-govindarajan
After loading the last post, and proofing it I spotted a small mistake.@Terry-R said:
Close the current tab, no need to save this file.
Click “open…”, you can do “File”, then “Open…” or the 2nd icon from left achieves the same result.Between these 2 steps, close Notepad++, then re-open it. This is because Notepad++ will only update the shortcuts.xml file on closing. You won’t find the “test” macro unless you close and re-open it.
Terry
-
Thanks for provide the detailed information’s.
I am just given the count just for as i have the more tags in that file. Sorry if my English may not be professional.
I didn’t think that the regex will help on this task.
Also i am working with ms-office and different kind of xml editors past 9 years. So i just doing the research for what thinks needs to be automate the different kind of tasks as i have faced in the past. Currently, i have the N++ editor and do research how this editor will make more effective and automate the task very efficiently. I just realized that the N++ having the lot of plugins which will use to automate the tasks very efficiently. That the think make join this community.
However i had tried these instructions as above but i am not able to save the shortcuts.xml after insert the macro code. I have tried both ways as, record dummy macro but not found/update in the shortcuts.xml and directly open the xml and paste the code as you provide above but not able to save it is asking the DTD path.
Please advise to proceed further.
Thanks again for your help!!
Ganesan. G
-
@ganesan-govindarajan , were you able to create the test macro I referred to as “tagtransfer”. When saving the recorded macro (step 9), then exiting Notepad++ and re-opening Notepad++ (this was a step I’d missed and added in another post), do you see that Macro listed under the Macro menu option?
If that step is not working then I currently don’t know what your issue is. How do you run Notepad++, is it as a normal account, or do you use “administrator” as there may be differences in how Notepad++ works in these 2 modes.
Terry
-
Yes. After added the “tagtransfer” macro i have closed the entire application and reopen it. Also i can able to see the macro under the Macro menu. I tried both normal and administrator account but still not found the macro in shortcuts.xml.
When i tried to add the macro manually in shortcuts.xml but asking the DTD.
Please advise.
-
@ganesan-govindarajan , so you have shown that you can write to a shortcuts.xml file. We now need to find which one you are using. Back in step 13 I said if the first shortcuts.xml file did NOT show the test macro, then look in 2 other locations. Try that now. If you find the test macro, complete the steps with that file.
You mention DTD, I don’t know what that means.
Terry
-
Now I can able to find the xml file in other location. I have updated the macro and replace the file in to the current Notepad++ installation folder.
I have followed the steps but nothing has changed in the patient file. I have added the 10 = without quotes in the second line of the patient file and ran the macro.
The Macro is running but there is no changes in the patient file.
Please advise.
ganesang. -
Did you add the “donor” file contents in line 1, above the 10 = ?
If so, then try the Replace function and copy the regex I provided in the previous posting (shown in red with find what and replace in front). Click on the find button and confirm that some of the donor file and patient file highlight. If so then the macro should also work.
Terry
-
-
Is there any regex to find text within the particular tag.
For example: the text “MDOK” needs to identify the child element.
-
At this point I’d suggest you start with reading the following post
https://notepad-plus-plus.org/community/topic/15765/faq-desk-where-to-find-regex-documentation It contains lots of helpful guidance on how to create regex’s.As the documentation points out, we aren’t a professional regex-writing forum, just people who enjoy using Notepad++ and yes regex, and also want to offer help to others who are learning.
So the rub of it is, we like people who ask questions to have at least tried to get the answer themselves. When they cannot solve it themselves they need to present a good representation of the data they are working with and hopefully what they’ve tried, be it a regex or if related to other aspects of Notepad++, any additional information.
I was interested in your original problem as it gave me a chance to work on a real issue and also to learn a bit more myself. I’m glad that once you DID follow the instructions it all worked out well. However with your latest request it sounds a bit like you expect us (forum contributors) to just be at your beck and call.
So with respect, I’d firstly suggest you do some reading, have a go at trying to solve the issue yourself and yes, if you cannot, then come back with some examples and what you have tried. Then maybe someone on the forum will be happy to help.
Terry
-
-