Tapatalk

Splitting XML on end tags

Splitting XML on end tags

74
Advanced UserAdvanced User
74

    Mar 24, 2021#1

    I'm so frustrated on this one. I use this function on another XML tag group and it works fine. The only thing I've changed is the starting tag string. So what this script is supposed to do. It selects the <emergproccl> to end tag </emergproccl>. Then spilts the found tags by end procedure tag </procedure> and creates new file for each </procedure>. Each new file has some other functions it performs, but that's not what's important. I need to understand why the <emergproccl> to </emergproccle> tags aren't splitting. Files attached. Thank you for the help.

    Test javascript

    Code: Select all

    var eSplitString = "</procedure>";
    // String where to split. The split occurs after next character.
    filePath = 'C:\\Test\\Bursting Documents\\TEST';
    // Path to put spliced files in
    //filePath = UltraEdit.getString("Directory to place files in", 1);
    //Append a backslash if it is missing at end of the directory string.
    if (filePath.match(/\\$/) === null) filePath += "\\";
    //UniqID = UltraEdit.getString("Enter a UNIQUE ID for this manual", 1);
    UniqID = "TEST";
    
    function emergproccl() {
        if (UltraEdit.document.length) { // Is any file open?
           // Set working environment required for this job.
           UltraEdit.insertMode();
           if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
           else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
           UltraEdit.activeDocument.hexOff();
           UltraEdit.perlReOn();
    
           // Move cursor to top of active file and run the initial search.
           UltraEdit.activeDocument.top();
           UltraEdit.activeDocument.findReplace.searchDown=true;
           UltraEdit.activeDocument.findReplace.matchCase=false;
           UltraEdit.activeDocument.findReplace.matchWord=false;
           UltraEdit.activeDocument.findReplace.regExp=true;
            UltraEdit.activeDocument.top();
    
           // Select everything in parent elements
            if (UltraEdit.activeDocument.findReplace.find('<emergproccl')) {
                var nLine1 = UltraEdit.activeDocument.currentLineNum;
                var nColumn1 = UltraEdit.activeDocument.currentColumnNum - 15;
                UltraEdit.activeDocument.findReplace.regExp = true;
                if (UltraEdit.activeDocument.findReplace.find("<\/emergproccl>")) {
                    UltraEdit.activeDocument.key("RIGHT ARROW");
                    if (UltraEdit.activeDocument.isSel()) {
                        // All the emergproccl elements are selectef from start tag to end tag.
                        UltraEdit.outputWindow.write("Inside Sel");
                        UltraEdit.activeDocument.gotoLine(0, UltraEdit.activeDocument.currentColumnNum - 15);
                    }
                    UltraEdit.activeDocument.gotoLineSelect(nLine1, nColumn1);
                }
                var emergproc = UltraEdit.activeDocument.selection;
    
                Regex = /(<emergproccl.*?>)(\r?\n)?<title>(.*?)<\/title>/i;
                match = Regex.exec(emergproc);
                if (match !== null) {
                    // Get emergproc starting tag and title tags
                    emergprocFullTag = match[0];
                    UltraEdit.outputWindow.write("emergprocFullTag " + emergprocFullTag);
                    // Get just the title
                    emergprocSTitle = match[3];
                    UltraEdit.outputWindow.write("emergprocSTitle " + emergprocSTitle);
                    if (emergprocSTitle !== "") {
                    //    legend.push(["\t" + emergprocSTitle]);
                    }
                }
                // Get the emergproc content as array of strings with splitting the
                // file on each occurrence of end tag of XML element procedure.
                var asProcedures = emergproc.split("</procedure>");
                if (!asProcedures)
                {
                    UltraEdit.activeDocument.top();
                }
    
            // The last string in array must be always ignored.
            var nProcedureCount = asProcedures.length - 1;
            UltraEdit.outputWindow.write("Emerg Proc Count: " + nProcedureCount);
            }
    
    
           // If the string to split is not found in this file, do nothing.
           if (UltraEdit.activeDocument.findReplace.find(eSplitString)) {
            UltraEdit.outputWindow.write("Inside eSplitString");
              var FileNumber = 1;    // Counts the number of saved files.
              var StringsFound = 1;  // Counts the number of found split strings.
              var NewFileIndex = UltraEdit.document.length;
              /* Get active file index in case of more than 1 file is open and the
                 current file does not get back the focus after closing the new files. */
              var FileToSplit = getActiveDocumentIndex();
              // Always use clipboard 9 for this script and not the Windows clipboard.
              UltraEdit.selectClipboard(9);
              // Split the file after every x found split strings until source file is empty.
              while (1) {
                 while (StringsFound < FoundsPerFile) {
                    if (UltraEdit.document[FileToSplit].findReplace.find("</procedure>")) StringsFound++;
                    else {
                       UltraEdit.document[FileToSplit].bottom();
                       break;
                    }
                 }
                 // End the selection of the find command.
                 UltraEdit.document[FileToSplit].endSelect();
                 // Move the cursor right to include the next character and unselect the found string.
                 UltraEdit.document[FileToSplit].key("RIGHT ARROW");
                 // Select from this cursor position everything to top of the file.
                 UltraEdit.document[FileToSplit].selectToTop();
                 // Is the file not already empty?
                 if (UltraEdit.document[FileToSplit].isSel()) {
                    // Cut the selection and paste it into a new file.
                    UltraEdit.document[FileToSplit].cut();
                    UltraEdit.newFile();
                    fileNum++;
                    UltraEdit.document[NewFileIndex].setActive();
                    setDTD();
    //                UltraEdit.activeDocument.write(placeHolderFront7900);
    //                UltraEdit.activeDocument.write(placeHolderNormproc7900);
                    UltraEdit.activeDocument.write(emergprocFullTag);
                    UltraEdit.activeDocument.paste();
                    titleStr = UniqID + "_" + fileNum;
                    UltraEdit.saveAs(filePath + titleStr + ".sgm");
                    fileNo.push("&" + titleStr + ";");
                    debug.write("File created: " + titleStr + ".sgm ");
                    StringsFound = 0;
                    UltraEdit.closeFile(UltraEdit.activeDocument.path, 2);
    
                    /* Delete the line termination in the source file
                       if last found split string was at end of a line. */
                    UltraEdit.document[FileToSplit].endSelect();
                    UltraEdit.document[FileToSplit].key("END");
                    if (UltraEdit.document[FileToSplit].isColNumGt(1)) {
                       UltraEdit.document[FileToSplit].top();
                    } else {
                       UltraEdit.document[FileToSplit].deleteLine();
                    }
                 } else break;
              }  // Loop executed until source file is empty!
    
              // Close source file without saving and re-open it.
              var NameOfFileToSplitE = UltraEdit.document[FileToSplit].path;
              UltraEdit.closeFile(NameOfFileToSplitE,2);
              /* The following code line could be commented if the source
                 file is not needed anymore for further actions. */
              UltraEdit.open(NameOfFileToSplitE);
    
              // Free memory and switch back to Windows clipboard.
              UltraEdit.clearClipboard();
              UltraEdit.selectClipboard(0);
           }
        }
    }
    emergproccl();
    Test SGM:

    Code: Select all

    <doc>
    <emergproccl>
    <title>YECURG DORMPEEEERSNC</title>
    <secttoc autobuild="1">
    <procedure emergency="0" verstatus="ver" newpage="1">
    <title>ATOBR</title>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">HTROLTTE</emphasis>
    </clitem>
    <action>
    <emphasis type="b">fsur eevlerl</emphasis>
    </action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">ERKSBA</emphasis>
    </clitem>
    <action>
    <emphasis type="b">pplay</emphasis>
    </action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <condition>CFD PPTRD EFP:ATIE ERALRAH RIA WFLS TERUIERAC</condition>
    <clstep1 verstatus="ver">
    <clitem>oe Cdnnotrlviie</clitem>
    <action>aft</action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>CigS nGioint</clitem>
    <action>oCdl</action>
    <cpos>P</cpos>
    </clstep1>
    <end>
    </procedure>
    <procedure emergency="0" verstatus="ver" newpage="1">
    <title>IIL TEAFUF  RRUNGLKLODTAOER IEFR</title>
    <condition>TNIS I FDOS OCEIOPT SIM :DAE</condition>
    <clstep1 verstatus="ver">
    <clitem>otrbA</clitem>
    <action>rpefrmo</action>
    <cpos>P</cpos>
    </clstep1>
    <condition>DIAS  EF CIED:NMOISIOCT OEUNN ITOFTF KEA</condition>
    <clstep1 verstatus="ver">
    <clitem>deninL graag</clitem>
    <action>otd nctotar er</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>alFps</clitem>
    <action>ras reqdeiu</action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>gasdit Linn hitWA hciFkletle craT </clitem>
    <action>ltocepem</action>
    <cpos>P</cpos>
    </clstep1>
    <end>
    </procedure>
    <procedure emergency="0" verstatus="ver" newpage="1">
    <title>UAKRIFER OEGD O IRTNFENOIGELA E FRRLNO EVTH</title>
    <condition> SECDIIFI DEPMION: TAO  SSOT</condition>
    <clstep1 verstatus="ver">
    <clitem>tboAr</clitem>
    <action>ferpomr</action>
    <cpos>P</cpos>
    </clstep1>
    <condition>S MIFIAN  DUDSEEI:CIOONTFN  E OTCATOIFKE</condition>
    <clstep1 verstatus="ver">
    <clitem>Cmlib</clitem>
    <action>ltaoietut fad se</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>ottlhreT</clitem>
    <action>eitqaaflrds ieu tuaeto rea diatt/tmninaas in</action>
    <cpos>P</cpos>
    </clstep1>
    <condition>SSAT OEOIIRRETT RKI.CNDECSRILEI  GTFRERR,GFSEEEHIEN  EED IKOINLEN  ECANDINHVG AU A NRCHQE CTCHFDKEO LVW CREEL</condition>
    <clstep1 verstatus="ver">
    <clitem>raftircA</clitem>
    <action>n snbipoelosossaa s  ald</action>
    <cpos>P</cpos>
    </clstep1>
    <end>
    </procedure>
    <procedure emergency="0" verstatus="ver" newpage="1">
    <title>GAELRAGWLN NIID C NTOATLR RET</title>
    <clstep1 verstatus="ver">
    <clitem>isariot nGpoe</clitem>
    <action>frVyie</action>
    <cpos>SO</cpos>
    </clstep1>
    <condition>ENTMOASD LSLGTI T /.  L ETN  I,T CALEEPFDRE I,ALH CRHCRGYCC ETOESRTEHXAHAIU WF FIRECKNE  EO UWS3TPCHLNKHOL TE SS O4L RIA RD HWLWNATTFEIHS  NSTODHTSF EID OTNWEAGENHI   HEAN IAIDG</condition>
    <condition>GNEIH E IME3 AOEPTNW XTGT FLEOHR LA 4NWO AEDD /RENTRAICI:FA NELSHSOT  R NS AWHTDH  EFR FEITN  OH OKI A IOSTITCNC</condition>
    <clstep1 verstatus="ver">
    <clitem>raeG</clitem>
    <action>dnwo</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>ratfircA</clitem>
    <action>saosoactinp rasc aladl  n</action>
    <cpos>P</cpos>
    </clstep1>
    <condition>NGDFTFIFEH LEOCESLNKA LI  E DRNUTOTL. FN L E  ILXIEGCESROY ERAITDWRLEOE X, DNA TA INGDETR</condition>
    <end>
    </procedure>
    <procedure emergency="0" verstatus="ver" newpage="1">
    <title> EENGDCNUFNYET HNR AGRIED MREO OPI/</title>
    <symbs>
    <symb>W</symb>
    </symbs>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">NIENOCLT ORDEIV</emphasis>
    </clitem>
    <action>
    <emphasis type="b">fat</emphasis>
    </action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>GET</clitem>
    <action>omceodreasnt eir</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>PMR</clitem>
    <action>o rsedctreoMeian</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>GgCniiotSn i</clitem>
    <action>docl</action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>retEccilalla od</clitem>
    <action>ceedru</action>
    <cpos>S O,P</cpos>
    <clstep2 verstatus="ver">
    <clitem> lL SnARxyca prt nleoopneorw</clitem>
    <action>fof</action>
    <cpos> ;OdPnS hsa&</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>iAd rmobmroneep ewor</clitem>
    <action>roaqeufiesr d,f </action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>adaPlyo</clitem>
    <action> saueqerird</action>
    <cpos>OS</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>aN rvntbh ittiangosieaols gd</clitem>
    <action>ffo</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>rodtneFFsI ranp</clitem>
    <action>off</action>
    <cpos>SP & nd;aOsh</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem> UoARACVr-dai102 </clitem>
    <action>foffi o n,qt uerierd</action>
    <cpos>n;Odhs P S&a</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>eoatPtht i</clitem>
    <action>sa reuiqedr</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>ehru tFleea</clitem>
    <action>f a ,osfdeueirqr</action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>proMweC M</clitem>
    <action>q oua, refdfseir</action>
    <cpos>P</cpos>
    </clstep2>
    <!--modified text from "SMS power" per FM scrub. KK 4/08/15-->
    <clstep2 verstatus="ver">
    <clitem>asInR  ee leesNhrto</clitem>
    <action>off</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>sNeh s eatolen</clitem>
    <action>%0</action>
    <cpos>P</cpos>
    </clstep2>
    </clstep1>
    <condition>STIPCATF NHRWH OREEECEINATCG OCKDLII HER TNSSERF  AINFST ISAOFNPTMN.EMPSOT NUE GLIAD DP,LNDD EARTTO ACI</condition>
    <end>
    </procedure>
    <procedure emergency="0" id="mq9_p0001" verstatus="ver" newpage="1">
    <title>EFNNGEI LUIEAR</title>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">ELGDI</emphasis>
    </clitem>
    <action>
    <emphasis type="b">tseshbail</emphasis>
    </action>
    <cpos>P</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">NILEI SGANTD</emphasis>
    </clitem>
    <action>
    <emphasis type="b">cleets</emphasis>
    </action>
    <cpos>P</cpos>
    <symbs>
    <symb>W</symb>
    </symbs>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>
    <emphasis type="b">NLODINIT OECERV</emphasis>
    </clitem>
    <action>
    <emphasis type="b">a,tfs  aqirdreue</emphasis>
    </action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <condition>ITEFTMITTITTSPO  REIAMS  NRT ( PMETNANDOCO IFE,TDARE ER R<xref xrefid="EArestart"> CEHICKLS.)T</condition>
    <condition>R NIS I SORFTIE P TEOOTLRASI SBS U,NSSCUUCFSELP AOR EERCPRFFOE RGNN.D ALDI</condition>
    <clstep1 verstatus="ver">
    <clitem>ticcreEllal aod</clitem>
    <action>rudece</action>
    <cpos>SP, O</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    <clstep2 verstatus="ver">
    <clitem>nprpowlocLnn ero S  eAaRylxt</clitem>
    <action>ffo</action>
    <cpos>Ssd P;O &han</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>eoiwrrbArone modme p</clitem>
    <action>qef efrui,orda s</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>oaydPal</clitem>
    <action>reas rieqdu</action>
    <cpos>OS</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>vNoai atrignoabeit sn lsdhgt</clitem>
    <action>ffo</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem> aFFInrtsponred</clitem>
    <action>fof</action>
    <cpos>dShs ;aO& Pn</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>Vi-AU2r0o  a1dRCA</clitem>
    <action>ir de,ffoif n otrequ</action>
    <cpos>O Ss Pd;anh&</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>ae toitPth</clitem>
    <action>ed rearqius</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>eFehul aetr</clitem>
    <action> faso ,ferquried</action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>MCMrow ep</clitem>
    <action>uf,r iefoq reasd</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem> etsoh INa Reesln</clitem>
    <action>fof</action>
    <cpos>P</cpos>
    </clstep2>
    <clstep2 verstatus="ver">
    <clitem>oNelse nh seat</clitem>
    <action>%0</action>
    <cpos>P</cpos>
    </clstep2>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>aBaptptlc erytiaoi inmte</clitem>
    <action>tiomonr</action>
    <cpos> SP,O</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>t yetarBevgalto</clitem>
    <action>oMtroni</action>
    <cpos> SP,O</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem> nelivCtnredoio</clitem>
    <action>qf ,aerut ardsei</action>
    <cpos>P</cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>r eu odKuxlteeilpvim</clitem>
    <action>udidn,tgosapiir z eq rmlnea eorfi</action>
    <cpos>S,P O</cpos>
    </clstep1>
    <clstep1 verstatus="ver">
    <clitem>erGa</clitem>
    <action>dsdo aq wner,iuer</action>
    <cpos>P
    </cpos>
    <symbs>
    <symb>C</symb>
    </symbs>
    </clstep1>
    <condition>VIREWEF ORCEL DAHETCGKN SDCNII LAN D<xref xrefid="SATCOMland">SS,   STCHAELICKDQREIREU.</condition>
    <end>
    </procedure>
    </emergproccl>
    </doc>
    ProcSplitErrors.zip (4.77 KiB)   0
    javascript and sgm file.

    6,685587
    Grand MasterGrand Master
    6,685587

      Mar 25, 2021#2

      There is a large selection made after execution of

      Code: Select all

      UltraEdit.activeDocument.gotoLineSelect(nLine1, nColumn1);
      Then after some work with the selected block there is executed

      Code: Select all

      if (UltraEdit.activeDocument.findReplace.find(eSplitString))
      This find is executed after end of current selection which is after </emergproccl> to end of the file. There is no string </procedure> from position after </emergproccl> up to bottom of the file. So the condition evaluates always to false and no file splitting is done at all.

      Insert above the IF condition:

      Code: Select all

      UltraEdit.activeDocument.top();
      Then the IF condition is true and the script processing continues inside the condition and JavaScript core reports an error in script because of FoundsPerFile is not defined at all.

      The function code up to the IF condition can be simplified to:

      Code: Select all

      function emergproccl() {
         if (UltraEdit.document.length < 1) return; // Is any file open?
      
         // Set working environment required for this job.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
         UltraEdit.activeDocument.hexOff();
         UltraEdit.perlReOn();
      
         // Move cursor to top of active file and run the initial search.
         UltraEdit.activeDocument.top();
         UltraEdit.activeDocument.findReplace.searchDown=true;
         UltraEdit.activeDocument.findReplace.matchCase=true;
         UltraEdit.activeDocument.findReplace.matchWord=false;
         UltraEdit.activeDocument.findReplace.regExp=false;
      
         // Select everything in parent elements
         if (!UltraEdit.activeDocument.findReplace.find('<emergproccl')) return;
         var nLine1 = UltraEdit.activeDocument.currentLineNum;
         var nColumn1 = UltraEdit.activeDocument.currentColumnNum - 12;
         if (!UltraEdit.activeDocument.findReplace.find("</emergproccl>")) return;
      
         UltraEdit.activeDocument.key("LEFT ARROW");
         UltraEdit.activeDocument.key("RIGHT ARROW");
         UltraEdit.activeDocument.gotoLineSelect(nLine1, nColumn1);
      
         // Get the title string of element title below element emergproccl.
         var emergproc = UltraEdit.activeDocument.selection;
         var emergprocSTitle = emergproc.replace(/^<emergproccl.*?>\s*<title>(.+?)<\/title>[\s\S]*$/g,"$1");
         if (emergprocSTitle.length == emergproc.length) emergprocSTitle = "No Title";
         UltraEdit.outputWindow.write("emergprocSTitle " + emergprocSTitle);
      
         // Get the emergproc content as array of strings with splitting the
         // file on each occurrence of end tag of XML element procedure.
         var asProcedures = emergproc.split("</procedure>");
      
         // The last string in array must be always ignored.
         asProcedures.pop();
         var nProcedureCount = asProcedures.length;
         UltraEdit.outputWindow.write("Emerg Proc Count: " + nProcedureCount);
      
         // Do nothing if the file does not contain procedure elements at all.
         if (!nProcedureCount) return;
      
      However, it is really unclear for me why selecting the block at all, why getting the title string, and how the file should be really split up.
      Best regards from an UC/UE/UES for Windows user from Austria

      74
      Advanced UserAdvanced User
      74

        Mar 26, 2021#3

        Hi Mofi
        Thanks for the feedback. So the script needs to select everything in a <normproccl>\s\S</normproccl> element. It must retrieve the <normproccl><title>. Then for every <procedure> in the <normproccl> Create a new file. In the new file the <normproccl><title> is added to the start of the file, followed by 10 <procedures>. So every new file would be like this.

        Code: Select all

        //Get the <normproccl> element and copy it with its <title> element for use on all new files created.
        <normproccl> <title>YECURG DORMPEEEERSNC</title> 
        <secttoc autobuild="1">
         //First XML to copy into a new file.
        <procedure emergency="0" verstatus="ver" newpage="1"> 
        <title>ATOBR</title> 
        <clstep1 verstatus="ver"> <clitem> <emphasis type="b">HTROLTTE</emphasis> </clitem>
        </procedure>
        //Second XML to copy into a new file.
        <procedure>
        <title>ATOBR</title>
        <clstep1> <clitem> <emphasis type="b">HTROLTTE</emphasis> </clitem>
        </procedure>
        Results after script

        Code: Select all

        //First new file
        <normproccl> <title>YECURG DORMPEEEERSNC</title>
        [size=50]//First XML to copy into a new file.[/size]
        <procedure emergency="0" verstatus="ver" newpage="1">
        <title>ATOBR</title>
        <clstep1 verstatus="ver"> <clitem> <emphasis type="b">HTROLTTE</emphasis> </clitem>
        </procedure>
        </normproccl>
        
        //Second XML to copy into a new file.
        <normproccl> <title>YECURG DORMPEEEERSNC</title>
        <procedure>
        <title>ATOBR</title>
        <clstep1> <clitem> <emphasis type="b">HTROLTTE</emphasis> </clitem>
        </procedure>
        </normproccl>
        So what needs to happen.
        1.  Select the whole <normproccl>.*?</normproccl> node. Store in a variable to perform searches in.
        2. Get the <normproccl><title>.*?<title> node. Store in a variable for use later.
        3. For each <procedure>.*?</procedure> found. Make a new file with it. Add the <normproccl><title>.*?<title> tags, then paste the <procedure> block into it.
        4. Do this for 10 <procedures found.
        5. Save file and move on to next <procedure> block.

        My new code splits up the <normproccl> group of elements, but the count is wrong. It's returning 8 files (80 procedures) when there are only 57 procedures in the file. I don't understand what's going wrong.

        Code: Select all

        function normproccl() {
            UltraEdit.activeDocument.top();
            // Set working environment required for this job.
            UltraEdit.insertMode();
            if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
            else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
            UltraEdit.activeDocument.hexOff();
            UltraEdit.perlReOn();
            // Move cursor to top of active file and run the initial search.
            UltraEdit.activeDocument.findReplace.searchDown=true;
            UltraEdit.activeDocument.findReplace.matchCase=false;
            UltraEdit.activeDocument.findReplace.matchWord=false;
            UltraEdit.activeDocument.findReplace.regExp=false;
        
            // Remove all content above the <normproccl tags.
        //    if (UltraEdit.activeDocument.findReplace.find('<normproccl.*?>')){
        //        UltraEdit.activeDocument.key("HOME");
        //        UltraEdit.activeDocument.selectToTop();
        //        UltraEdit.activeDocument.deleteText();
        //    }
        //
        //    // Remove all content below the <normproccl end tag.
        //    if (UltraEdit.activeDocument.findReplace.find('<\/normproccl>')){
        //        UltraEdit.activeDocument.key("RIGHT ARROW");
        //        UltraEdit.activeDocument.selectToBottom();
        //        UltraEdit.activeDocument.deleteText();
        //    }
        
            UltraEdit.activeDocument.findReplace.regExp=true;
            // Go back to the start of file then search
            UltraEdit.activeDocument.top();
        
            // Select everything in parent elements
            if (!UltraEdit.activeDocument.findReplace.find('<normproccl')) return;
            UltraEdit.activeDocument.key("LEFT ARROW");
            var nLine1 = UltraEdit.activeDocument.currentLineNum;
            var nColumn1 = UltraEdit.activeDocument.currentColumnNum - 12;
            if (!UltraEdit.activeDocument.findReplace.find("</normproccl>")) return;
            UltraEdit.activeDocument.key("LEFT ARROW");
            UltraEdit.activeDocument.key("RIGHT ARROW");
            UltraEdit.activeDocument.gotoLineSelect(nLine1, nColumn1);
        
            var normproc = UltraEdit.activeDocument.selection;
        
        
            // Get the title string of element title below element emergproccl.
            var normREGEX = /(<normproccl.*?>\s*<title>(.+?)<\/title>\s*<secttoc autobuild="\d+">)/im;
            var match = normREGEX.exec(normproc);
            if (match != null) {
                normTitleFullTags = match[1];
                normSTitle = match[2];
            } else {
                normTitleFullTags = "";
            }
        
            // Push emerg Title string into Toc legend[]
            if (normSTitle !== "") {
                legend.push(["\t" + normSTitle]);
            }
            if (normSTitle.length == normproc.length) normSTitle = "No Title";
        
        
            // Get the emergproc content as array of strings with splitting the
            // file on each occurrence of end tag of XML element procedure.
            var asProcedures = normproc.split("</procedure>");
            // The last string in array must be always ignored.
            asProcedures.pop();
            var nProcedureCount = asProcedures.length;
            UltraEdit.outputWindow.write("Normproc Proc Count: " + nProcedureCount);
            // Do nothing if the file does not contain procedure elements at all.
            if (!nProcedureCount) return;
        
            if (UltraEdit.activeDocument.findReplace.find(SplitString)){
                // set variable for how many procedures each file will have.
                var FoundsPerFile = 10;
                // Counts the number of saved files.
                FileNumber++
                // Counts the number of found split strings.
                var StringsFound = 1;
                var NewFileIndex = UltraEdit.document.length;
                /* Get active file index in case of more than 1 file is open and the
                current file does not get back the focus after closing the new files. */
                FileToSplit = getActiveDocumentIndex();
                // Always use clipboard 9 for this script and not the Windows clipboard.
                UltraEdit.selectClipboard(9);
                // Split the file after every 10 procedure split strings until source file is empty.
                while (1) {
                    while (StringsFound < FoundsPerFile) {
                        if (UltraEdit.document[FileToSplit].findReplace.find(SplitString)){
                            StringsFound++;
                        }
                        else {
                            UltraEdit.document[FileToSplit].bottom();
                            break;
                        }
                    }
                    // End the selection of the find command.
                    UltraEdit.document[FileToSplit].endSelect();
                    // Move the cursor right to include the next character and unselect the found string.
                    UltraEdit.document[FileToSplit].key("RIGHT ARROW");
                    // Select from this cursor position everything to top of the file.
                    UltraEdit.document[FileToSplit].selectToTop();
                    // Is the file not already empty?
                    if (UltraEdit.document[FileToSplit].isSel()) {
                        // Cut the selection and paste it into a new file.
                        UltraEdit.document[FileToSplit].cut();
                        UltraEdit.newFile();
                        UltraEdit.document[NewFileIndex].setActive();
                        setDTD();
                        UltraEdit.activeDocument.write(placeHolderFront7900);
                        if(FileNumber === 1){
                            UltraEdit.activeDocument.paste();
        
                        }else{
                            UltraEdit.activeDocument.write("\r\n" + normTitleFullTags + "\r\n");
                            UltraEdit.activeDocument.paste();
                        }
                        UltraEdit.activeDocument.write("</normproccl>");
                        UltraEdit.activeDocument.write(placeHolderEmergproc7900);
        
                        /* Add line termination on the last line and remove automatically added indent
                       spaces/tabs if auto-indent is enabled if the last line is not already terminated. */
                        if (UltraEdit.activeDocument.isColNumGt(1)) {
                           UltraEdit.activeDocument.insertLine();
                           if (UltraEdit.activeDocument.isColNumGt(1)) {
                              UltraEdit.activeDocument.deleteToStartOfLine();
                           }
                        }
                        UltraEdit.saveAs(filePath + titleStr + ".sgm");
                        fileNo.push("&" + titleStr + ";");
                        debug.write("File created: " + titleStr + ".sgm ");
                        UltraEdit.closeFile(UltraEdit.activeDocument.path, 2);
                        fileNum++;
                        FileNumber++;
                        StringsFound = 0;
                    /* Delete the line termination in the source file
                       if last found split string was at end of a line. */
        /*                UltraEdit.document[FileToSplit].endSelect();
                        UltraEdit.document[FileToSplit].key("END");
                        if (UltraEdit.document[FileToSplit].isColNumGt(1)) {
                           UltraEdit.document[FileToSplit].top();
                        } else {
                           UltraEdit.document[FileToSplit].deleteLine();
                        }*/
                    } else break;
                }  // Loop executed until source file is empty!
        
            }
        }
        
        normproccl();
        Example SGML file attached.

        I hope my explanation makes it clearer. If not let me know and I'll think of another way to word it.
        As always thank you for the help and guidance.
        Max
        normProcTest.zip (9.04 KiB)   0
        Script
        ExampleNormproc.zip (49.36 KiB)   0
        SGML demo

          Solution

          Mar 31, 2021#4

          Just in case anyone is following this thread here's the solution.

          Code: Select all

          function emergproccl() {
              UltraEdit.activeDocument.top()
              UltraEdit.activeDocument.findReplace.mode=0;
              UltraEdit.activeDocument.findReplace.matchCase=false;
              UltraEdit.activeDocument.findReplace.matchWord=false;
              UltraEdit.activeDocument.findReplace.regExp=false;
              UltraEdit.activeDocument.findReplace.searchDown=true;
              UltraEdit.activeDocument.findReplace.searchInColumn=false;
              if(UltraEdit.activeDocument.findReplace.find("<emergproccl")){
                  UltraEdit.activeDocument.key("LEFT ARROW");
                  var eLine1 = UltraEdit.activeDocument.currentLineNum;
                  var eColumn1 = UltraEdit.activeDocument.currentColumnNum - 15;
                  if (!UltraEdit.activeDocument.findReplace.find("</emergproccl>")) return;
                  UltraEdit.activeDocument.key("LEFT ARROW");
                  UltraEdit.activeDocument.key("RIGHT ARROW");
                  UltraEdit.activeDocument.gotoLineSelect(eLine1, eColumn1);
              }
          
              var emergproc = UltraEdit.activeDocument.selection;
          
              // Get the title string of element title below element normproccl.
              var emergprocREGEX = /(<emergproccl.*?>\s*<title>(.+?)<\/title>\s*<secttoc autobuild="\d+">)/;
              var match = emergprocREGEX.exec(emergproc);
              if (match != null) {
                  emergprocTitleFullTags = match[1];
                  emergprocSTitle = match[2];
              } else {
                  emergprocTitleFullTags = "";
                  emergprocSTitle = "";
              }
          
              if (emergprocSTitle.length == emergproc.length) emergprocSTitle = "No Title";
          
              UltraEdit.newFile();
              UltraEdit.activeDocument.write(emergproc);
              UltraEdit.activeDocument.top();
              // Get the emergproc content as array of strings with splitting the
              // file on each occurrence of end tag of XML element procedure.
              var asProcedures = emergproc.split("</procedure>");
              var nProcedureCount = asProcedures.length;
              //UltraEdit.outputWindow.write("Emergproc Count: " + nProcedureCount);
              var emergFileCount = nProcedureCount / 10;
              // returns the largest filecount number if it's a decimal
              eFileCount = Math.ceil(emergFileCount)
              eTotFileCount = nFileCount + eFileCount;
              eTotFileCount = Math.ceil(eTotFileCount)
          
              //debug.write("Total N/E file count " + eTotFileCount);
              var FoundsPerFile = 10;
              if (UltraEdit.activeDocument.findReplace.find(SplitString)){
                  // Counts the number of found split strings.
                  var StringsFound = 1;
                  var NewFileIndex = UltraEdit.document.length;
                  /* Get active file index in case of more than 1 file is open and the
                  current file does not get back the focus after closing the new files. */
                  FileToSplit = getActiveDocumentIndex();
                  // Always use clipboard 9 for this script and not the Windows clipboard.
                  UltraEdit.selectClipboard(9);
                  while (1) {
                      while (StringsFound < FoundsPerFile) {
                          if (UltraEdit.document[FileToSplit].findReplace.find(SplitString)){
                              StringsFound++;
                          } else {
          
                          if (UltraEdit.document[FileToSplit].findReplace.find("</emergproccl>"));
                              tailEndTags = true;
                              UltraEdit.document[FileToSplit].bottom();
                              UltraEdit.document[FileToSplit].deleteText();
                              StringsFound === FoundsPerFile;
                              break;
                          }
                      }
                      // End the selection of the find command.
                      UltraEdit.document[FileToSplit].endSelect();
                      // Move the cursor right to include the next character and unselect the found string.
                      UltraEdit.document[FileToSplit].key("RIGHT ARROW");
                      // Select from this cursor position everything to top of the file.
                      UltraEdit.document[FileToSplit].selectToTop();
                      // Is the file not already empty?
                      if (UltraEdit.document[FileToSplit].isSel()) {
                          // Cut the selection and paste it into a new file.
                          UltraEdit.document[FileToSplit].cut();
                          UltraEdit.newFile();
                          UltraEdit.document[NewFileIndex].setActive();
          
                          var multiStringsFound = 1;
                          // first found procedure
                          if(FileNumber === lastNormFileNo){
                              UltraEdit.activeDocument.write("<?Pub start>\r\n");
                              UltraEdit.activeDocument.paste();
                              UltraEdit.activeDocument.write("<?Pub stop>");
                              UltraEdit.activeDocument.write("</emergproccl>");
                              multiStringsFound++;
                              // if filenumber == total file count
                          } else if (FileNumber  === eTotFileCount) {
                              UltraEdit.activeDocument.write(emergprocTitleFullTags + "\r\n");
                              UltraEdit.activeDocument.write("<?Pub start>\r\n");
                              UltraEdit.activeDocument.paste();
                              UltraEdit.activeDocument.write("<?Pub stop>\r\n");
                          }else{
                              if (multiStringsFound < FoundsPerFile){
                                  UltraEdit.activeDocument.write(emergprocTitleFullTags + "\r\n");
                                  UltraEdit.activeDocument.write("<?Pub start>\r\n");
                                  UltraEdit.activeDocument.paste();
                                  UltraEdit.activeDocument.write("<?Pub stop>\r\n");
                                  UltraEdit.activeDocument.write("</emergproccl>");
                                  multiStringsFound++;
                              }
                          }
          
                          /* Add line termination on the last line and remove automatically added indent
                         spaces/tabs if auto-indent is enabled if the last line is not already terminated. */
                          if (UltraEdit.activeDocument.isColNumGt(1)) {
                             UltraEdit.activeDocument.insertLine();
                             if (UltraEdit.activeDocument.isColNumGt(1)) {
                                UltraEdit.activeDocument.deleteToStartOfLine();
                             }
                          }
          
                          titleStr = UniqID + "_" + FileNumber;
          
                          UltraEdit.saveAs(filePath + titleStr + ".sgm");
                          debug.write("FILE : " + filePath + titleStr + ".sgm");
                          FileNumber++;
                          UltraEdit.closeFile(UltraEdit.activeDocument.path, 2);
                          StringsFound = 0;
                      } else break;
                  }  // Loop executed until source file is empty!
              }
          }
          😊