Auto numbering IDs in an XML file in several levels

Auto numbering IDs in an XML file in several levels

3
NewbieNewbie
3

    Oct 21, 2015#1

    Here's my problem... I have an id that looks like:

    Code: Select all

    ins1proc1s1
    ins1 represents the top level tag
    proc1 represents the first proc inside of the ins
    s1 represents the steps inside the proc

    I need a script to make the following happen:

    Code: Select all

    ins1proc1s1
    ins1proc1s2
    ins1proc1s3
    
    ins1proc2s1
    ins1proc2s2
    ins1proc2s3
    
    ins1proc3s1
    ins1proc3s2
    ins1proc3s3
    
    ins2proc1s1
    ins2proc1s2
    ins2proc1s3
    
    ins2proc2s1
    ins2proc2s2
    ins2proc2s3
    And so on. I can't use column mode because there is a lot of text in between the ids, and depending on the level and what tags I'm using, the ids may or may not line up with the numbers. My example ID is a VERY basic version of what I'm actually using.

    Is it possible to increment the s numbers inside each proc, then each proc inside each ins, then each ins, starting over with s and proc within each group? Does that even make sense? I tried to expand on the regular expressions example I was given in my last question (thank you!), but I can't seem to make it increment the numbers.

    6,687586
    Grand MasterGrand Master
    6,687586

      Oct 21, 2015#2

      Yes, of course this is possible with a script as it can be seen on topics found with advanced search (link at top right corner) with search string increment* number* with Scripts forum selected.

      What I miss on your task description is the identifier for the blocks. How should the script find out where procX block ends and where procY begins?

      Is an empty line (no characters in line) or a blank line (no or just whitespace characters in line) the separator between the blocks?

      And where does an ins block begin and end?

      Perhaps a real data example would be better as a simplified example.
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Oct 21, 2015#3

        Sometimes we use different tags, or have step2 and step3. We can't use column mode because, as you can see, depending on the tag, they don't line up. An example of the structure would be:

        Code: Select all

        <inspect id="M00004_TM-1-2345-678_ins1">
        <title></title>
        <proc id="M00004_TM-1-2345-678_ins1proc1">
        <title></title>
        <step1 id="M00004_TM-1-2345-678_ins1proc1s1">
        <para>General information steps.</para>
        </step1>
        <step1 id="M00004_TM-1-2345-678_ins1proc1s2">
        <para>General information steps.</para>
        </step1>
        </proc>
        <proc id="M00004_TM-1-2345-678_ins1proc2">
        <title></title>
        <step1 id="M00004_TM-1-2345-678_ins1proc2s1">
        <para>General information steps.</para>
        </step1>
        <step1 id="M00004_TM-1-2345-678_ins1proc2s2">
        <para>General information steps.</para>
        </step1>
        </proc>
        </inspect>
        <inspect id="M00004_TM-1-2345-678_ins2">
        <title></title>
        <proc id="M00004_TM-1-2345-678_ins2proc1">
        <title></title>
        <step1 id="M00004_TM-1-2345-678_ins2proc1s1">
        <para>General information steps.</para>
        </step1>
        <step1 id="M00004_TM-1-2345-678_ins2proc2s2">
        <para>General information steps.</para>
        </step1>
        </proc>
        <proc id="M00004_TM-1-2345-678_ins2proc2">
        <title></title>
        <step1 id="M00004_TM-1-2345-678_ins2proc2s1">
        <para>General information steps.</para>
        </step1>
        <step1 id="M00004_TM-1-2345-678_ins2proc2s2">
        <para>General information steps.</para>
        </step1>
        </proc>
        </inspect>

        6,687586
        Grand MasterGrand Master
        6,687586

          Oct 21, 2015#4

          Try following quickly coded script. It should work on small files making all changes in memory.

          Code: Select all

          if (UltraEdit.document.length > 0)  // Is any file opened?
          {
             // Define environment for this script.
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
             // Select the entire file contents.
             UltraEdit.activeDocument.selectAll();
             if (UltraEdit.activeDocument.isSel())
             {
                // Load file into memory as an array of strings with
                // using end tag of element inspect as separator.
                var asInspects = UltraEdit.activeDocument.selection.split("</inspect");
          
                // Loop running on each inspect element.
                for (var nInspect = 0; nInspect < asInspects.length; nInspect++)
                {
                   // Replace all "_ins" with any number appended case-sensitive
                   // by "_ins" with correct inspect element number appended using
                   // replace function of JavaScript core object String with first
                   // parameter being a JavaScript regular expression object.
                   // (Perl syntax, but not as powerful as Perl regular expression
                   // engine of UltraEdit. Don't mix JavaScript Perl RegEx object
                   // with Boost Perl regular expression engine of UltraEdit).
                   var sNumber = "_ins" + (nInspect+1).toString(10);
                   asInspects[nInspect] = asInspects[nInspect].replace(/_ins\d+/g,sNumber);
          
                   // Split up current inspect element into proc elements.
                   var asProcs = asInspects[nInspect].split("</proc");
          
                   // Loop running on each proc element.
                   for (var nProc = 0; nProc < asProcs.length; nProc++)
                   {
                      var sNumber = "proc" + (nProc+1).toString(10);
                      asProcs[nProc] = asProcs[nProc].replace(/proc\d+/g,sNumber);
          
                      // Split up current proc element into step elements.
                      var asSteps = asProcs[nProc].split("</step");
          
                      // Loop running on each step element.
                      for (var nStep = 0; nStep < asSteps.length; nStep++)
                      {
                         var sNumber = "$1" + (nStep+1).toString(10);
                         asSteps[nStep] = asSteps[nStep].replace(/(proc\d+s)\d/g,sNumber);
                      }
                      // Join the step elements of current proc element.
                      asProcs[nProc] = asSteps.join("</step");
                   }
                   // Join the proc elements of current inspect element.
                   asInspects[nInspect] = asProcs.join("</proc");
                }
                // Overwrite existing selection by renumbered and joined inspect elements.
                UltraEdit.activeDocument.write(asInspects.join("</inspect"));
             }
          }
          
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Oct 21, 2015#5

            That works GREAT.

            If I want to change the tags for it, I'd just replace where they're each called out, or add inside the levels if there were more tags?

            6,687586
            Grand MasterGrand Master
            6,687586

              Oct 22, 2015#6

              I think, the principle of how the replaces are made on the hierarchical data is easy to see and to understand.

              Yes, just the tags, the search/replace strings and the level of for loops must be adapted for different XML contents.
              Best regards from an UC/UE/UES for Windows user from Austria