2

I have an xml-data, which contains a lot of different tags. The structure is like this:

<category>
  <lesson>
    <textfilecard orderID="0">
      <Question></Question>
      <Answer></Answer>
    </textfilecard>
  </lesson>
</category>

It's an export from my flash card program. As you can imagine, there are multiple "textfilecards" which all have a "orderID". I want to merge two of my exports and change the "orderID" of those incrementally in NotePad++ or whatever tool is good for it. How can I achieve that?

Search and Replace would just replace the "orderID" with the same value over and over, so I can't use that. Is there another trick?

So basically, I want this:

    <textfilecard orderID="1">
    <textfilecard orderID="2">
    <textfilecard orderID="3">
    <textfilecard orderID="1">
    <textfilecard orderID="2">
    <textfilecard orderID="3">

...to change into this in an easy way:

    <textfilecard orderID="1">
    <textfilecard orderID="2">
    <textfilecard orderID="3">
    <textfilecard orderID="4">
    <textfilecard orderID="5">
    <textfilecard orderID="6">

2 Answers 2

1

First, on the menu, click Plugin -> Plugin Admin. Search Python script and install it. After the installation, click Plugin -> PythonScript -> New Script. Enter a filename, then enter the code below:

import re

counter = 0
def calculate(match):
    global counter
    counter += 1
    return str(counter)

editor.rereplace('~', calculate)

The code replaces ~ with numbers. (I have modified @Toto 's code from a previous answer).

Second, Press Ctrl+H.

  • Find what: \d+
  • Replace with: ~
  • Search mode: Regular expression Click Replace All. All numbers will be replaced with ~.

Thirdly, click Plugin -> PythonScript, select the file create on the first step. All ~ will be replaced with numbers. The numbers will be incremented sequentially.

4
  • Oh, this is good to know, but I think, I kinda failed at explaining my problem. My problem looks more like this: <category> <lesson> <textfilecard orderID="0"> <Question></Question> <Answer></Answer> </textfilecard> <textfilecard orderID="1"> <Question></Question> <Answer></Answer> </textfilecard> <textfilecard orderID="0"> <Question></Question> <Answer></Answer> </textfilecard> <textfilecard orderID="1"> <Question></Question> <Answer></Answer> </textfilecard> </lesson> </category>
    – Feteo
    Oct 3, 2020 at 16:38
  • Hmm, it doesn't display it the way I want it to be. Anyway, think of it like this: There are some other lines between those tags as well.
    – Feteo
    Oct 3, 2020 at 16:39
  • @Feteo See edit Oct 3, 2020 at 19:12
  • Thank you. This helped!
    – Feteo
    Oct 6, 2020 at 23:52
0

The easiest way to do this is by using Excel, with text to columns. Delimit on ", change the values using a formula (cell above, +1) then copy it down. You can then concatenate the values back together again.

Text to columns can be found on the 'Data' tab, and would render the output like this.

This is the output of the text to columns

Enter the formula into B2 and drag it down

enter image description here

And then concatenate with the " entered back in like this

enter image description here

You can then paste the concatenated output back into Notepad++. Getting it nested back into the correct XML pattern with the headings you have is not something I know how to do. Maybe you could paste the whole lot into Excel, and do something similar to the above as it'll only delimit on "

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .