Need help to replace tags to one xml from another xml
-
Thanks for reply Terry.
Yes you we’re correct. Every entries with text <desc>abcd</desc> will be move to the respective empty tags <desc><desc> vise vara.
The total count of both entries with and without texts are same.
So we can move or replace the empty tags with texts vise vara.
Please advise code for this?
Thanks
Ganesan. G -
If you suggest if any vbscript coding also to replace the tags via activex plugin which will be helful …
-
So as an example, here might be some data:
First (source) xml file:
Why half <desc>stone</desc> summer insect planet are sing turn caught poor. From sleep began crowd it; stand condition push give short. Value sound foot about <desc>proper</desc> hair animal; metal crop region, forest. Bright farm bell eye choose fill populate <desc>dream</desc> fit know has came. By claim list <desc>travel</desc> plural art town clothe gave, enter oh eat shoe love found.
Second (destination) xml file:
Heavy talk book plain <desc></desc> century snow, happy home especially, do, fig box. Age, walk ring watch kill <desc></desc> path thus just all. Half depend room word <desc></desc> smile mean; segment crop seem. Famous less order up change matter felt had vary until. Size half general stay get picture for <desc></desc> stoned even dollar sure people!
Resulting file after desired replacement:
Heavy talk book plain <desc>stone</desc> century snow, happy home especially, do, fig box. Age, walk ring watch kill <desc>proper</desc> path thus just all. Half depend room word <desc>dream</desc> smile mean; segment crop seem. Famous less order up change matter felt had vary until. Size half general stay get picture for <desc>travel</desc> stoned even dollar sure people!
I think that may be representative…
-
I’m not a coder of vbscript, python or anything similar to that. I can deal with regular expressions (regex), that’s my limit currently.
Could you provide me with a count of the number of <desc></desc> entries in the “donor” file. To do so, open the “donor” file in Notepad++. Then use the “Find” option (under “Search”).
In the Find What: field type
(?s)<desc>.+?</desc>
and let me know the number when you click on the “Count” button.As I stated earlier, the regex will only swap 1 entry at a time. That means running it multiple times. If the number is low enough you could just manually run the regex until a message “0 occurrences…” showed… If the number is high then it would likely benefit from being created as a macro, then you can run the macro ‘x’ times using the menu.
Terry
-
The source file doesn’t have any texts or paragraphs so shows likely as follows,
<desc>abcd</desc>
<desc>defg</desc>
<desc>mnop</desc> etc…So we just have to copy or cut and paste in to the destination file.
-
Thanks for the reply. I have count total 225 or more. So not able to do one by one manually.
So we have to automate this task, but the the macro not do exactly as we record.
Please advise if you have any other options.
-
As @Terry-R said early on, “To my mind this is a job for a bit of programming, possibly python.” This is not inherently a Notepad++ issue.
This forum is primarily for help with Notepad++ and its specifics; we also often give regex help, because many users of NPP have not been exposed to regex, and it’s one of the powerful features in NPP, but it’s not actually the primary focus of the forum (and a regex alone doesn’t seem to be sufficient for your needs); we also sometimes give help with PythonScript (a plugin which gives an embedded Python interpreter direct access to features of the Notepad++ GUI and the underlying editor components), but that help tends to focus on the specifics of getting specific tasks done in the NPP interface. Your question is more on the generic programming end of things; it could be done in Python in general (and thus in PythonScript plugin), but really, it sounds more like someone inside your company with programming experience should help you with.
If you showed some effort, there are probably some here who could help you tweak what you have tried. But so far, you’ve just handed out tidbits of new information very slowly, and repeatedly asked us to do it for you. For those of us who are here, using our own time to help other users of Notepad++, it sounds very much like you’ve just asked us to do your homework for you, or (even worse), to do for free the job you’re being paid to do. Neither of those sit well.
You claim you had a macro, but it didn’t do exactly what you wanted. Perhaps you could give us a copy of that macro (look in
%AppData%\Notepad++\shortcuts.xml
for a normal Notepad++ install), plus fake-data copies of the “source” and “destination” files (since you indicated the ones Scott came up with were not representative, you need to show us something that is, rather than making us guess). If you show that much effort, we might be able to help you figure out what’s going wrong with the macro, and help you tweak it. Or, with that new amount of effort shown, maybe someone will take pity on you and write it in PythonScript for you.(PS: to embed the macro, or your example data, you can indent every line four spaces, and put a blank line before and after it. You can also see Scott’s markdown-in-this-forum cheat sheet)
-
@PeterJones , yes I’ve maintained that all I will provide is a regex (possibly built as a macro). This last reply where @ganesan-govindarajan mentions a macro not working properly I’m intrigued about. I wonder if English not being his first language he was actually mistaken on the regex I supplied to do a count.
@ganesan-govindarajan
Regardless, given the count of 225 (or more) I will be providing the regex as a macro. I will provide instructions on how to edit the shortcuts.xml file to insert the code. Other instructions will be provided to explain how both files must be loaded into Notepad++ so that the regex works as expected.The regex can also be altered if required to fit other tags, I can provide that information as well if you think it might help with other situations.
So be a bit more patient, I have the regex, now I have to convert it to a macro, test again, then provide that information on this forum for you.
Terry
-
@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
-
-