How to expand xref rid in XML with a script?

How to expand xref rid in XML with a script?

62
Advanced UserAdvanced User
62

    13:56 - Aug 10#1

    Hi all,

    I am trying to expand xref rid value based on specific reference type. I am trying to create a script which is attached.

    Sample input:

    Code: Select all

    <xref ref-type="bibr" rid="ref2_33">33</xref>&#x2013;<xref ref-type="bibr" rid="ref2_39">39</xref>
    <xref ref-type="bibr" rid="ref2_33">Refs. 33</xref>&#x2013;<xref ref-type="bibr" rid="ref2_39">39</xref>
    The output after script execution is:

    Code: Select all

    <xref ref-type="bibr" rid="ref2_33 ref2_34 ref2_35 ref2_36 ref2_37 ref2_38 ref2_39">33&#x2013;39</xref>
    <xref ref-type="bibr" rid="">Refs. 33&#x2013;39</xref>
    The expected output should be:

    Code: Select all

    <xref ref-type="bibr" rid="ref2_33 ref2_34 ref2_35 ref2_36 ref2_37 ref2_38 ref2_39">33&#x2013;39</xref>
    <xref ref-type="bibr" rid="ref2_33 ref2_34 ref2_35 ref2_36 ref2_37 ref2_38 ref2_39">Refs. 33&#x2013;39</xref>
    Can anyone help me...
    expand_rid_in_xml.zip (947 Bytes)   0

      9:51 - Aug 11#2

      Sorry for not providing initially the expected output which is added now to my first post.

      The main purpose of the script is to link all the range links linked separately in the file together and expand the RID value of the xref link.

      I have some more examples below:

      Code: Select all

      -----------------------------
      <xref ref-type="bibr" rid="ref1_33">33</xref>&#x2013;<xref ref-type="bibr" rid="ref1_39">39</xref>
      s/b
      <xref ref-type="bibr" rid="ref1_33 ref1_34 ref1_35 ref1_36 ref1_37 ref1_38 ref1_39">33&#x2013;39</xref>
      
      <xref ref-type="bibr" rid="ref1_33">Refs. 33</xref> to <xref ref-type="bibr" rid="ref1_39">39</xref>
      s/b
      <xref ref-type="bibr" rid="ref1_33 ref1_34 ref1_35 ref1_36 ref1_37 ref1_38 ref1_39">Refs. 33 to 39</xref>
      
      <xref ref-type="fig" rid="fig1_1">Figure 1.1A-D</xref>&#x2013;<xref ref-type="fig" rid="fig1_5">Figure 1.5C</xref>
      s/b
      <xref ref-type="fig" rid="fig1_1 fig1_2 fig1_3 fig1_4 fig1_5">Figure 1.1A-D&#x2013;Figure 1.5C</xref>
      
      
      <xref ref-type="table" rid="apptab1">Appendix Tables 1(a)</xref>&#x2013;<xref ref-type="table" rid="apptab12">12(c,d)</xref>
      s/b
      <xref ref-type="table" rid="apptab1 apptab2 apptab3 apptab4 apptab5 apptab6 apptab7 apptab8 apptab9 apptab10 apptab11 apptab12">Appendix Tables 1(a)&#x2013;12(c,d)</xref>
      
      <xref ref-type="fn" rid="fn1_1"><sup>1</sup></xref>&#x2013;<xref ref-type="fn" rid="fn1_5"><sup>5</sup></xref>
      s/b
      <xref ref-type="fn" rid="fn1_1 fn1_2 fn1_3 fn1_4 fn1_5"><sup>1&#x2013;5</sup></xref>
      
      <sup><xref ref-type="fn" rid="fn1_10">10</xref>&#x2013;<xref ref-type="fn" rid="fn1_15">15</xref></sup>
      s/b
      <xref ref-type="fn" rid="fn1_10 fn1_11 fn1_12 fn1_13 fn1_14 fn1_15"><sup>10&#x2013;15</sup></xref>
      
      <xref ref-type="math" rid="eqn1_1">Eqns. 1.1</xref>&#x2013;<xref ref-type="math" rid="eqn1_5">1.5</xref>
      s/b
      <xref ref-type="math" rid="eqn1_1 eqn1_2 eqn1_3 eqn1_4 eqn1_5">Eqns. 1.1&#x2013;1.5</xref>
      
      <xref ref-type="book-part" rid="chapter9">Chapters 9</xref>&#x2013;<xref ref-type="book-part" rid="chapter11">11</xref>
      s/b
      <xref ref-type="book-part" rid="chapter9 chapter10 chapter11">Chapters 9&#x2013;11</xref>
      
      <xref ref-type="boxed-text" rid="box1_1">Box 1.1</xref> through <xref ref-type="boxed-text" rid="box1_3">1.3</xref>
      s/b
      <xref ref-type="boxed-text" rid="box1_1 box1_2 box1_3">Box 1.1&#x2013;1.3</xref>
      
      -----------------------------
      <xref ref-type="fig" rid="fig2.5c">Figs. 2.5c</xref>-<xref ref-type="fig" rid="fig2.5g">g</xref>
      s/b
      <xref ref-type="fig" rid="fig2.5c fig2.5d fig2.5e fig2.5f fig2.5g">Figs. 2.5c-g</xref>
      -----------------------------
      <xref ref-type="book-part" rid="chapterII">Chapter II</xref> to <xref ref-type="book-part" rid="chapterV">V</xref>
      s/b
      <xref ref-type="book-part" rid="chapterII chapterIII chapterIV chapterV">Chapters II&#x2013;V</xref>
      -----------------------------

      6,675585
      Grand MasterGrand Master
      6,675585

        13:16 - Aug 16#3

        The attached ZIP file contains the enhanced script working for:

        Code: Select all

        -----------------------------
        <xref ref-type="bibr" rid="ref1_33">33</xref>&#x2013;<xref ref-type="bibr" rid="ref1_39">39</xref>
        s/b
        <xref ref-type="bibr" rid="ref1_33 ref1_34 ref1_35 ref1_36 ref1_37 ref1_38 ref1_39">33&#x2013;39</xref>
        
        <xref ref-type="bibr" rid="ref1_33">Refs. 33</xref> to <xref ref-type="bibr" rid="ref1_39">39</xref>
        s/b
        <xref ref-type="bibr" rid="ref1_33 ref1_34 ref1_35 ref1_36 ref1_37 ref1_38 ref1_39">Refs. 33 to 39</xref>
        
        <xref ref-type="fig" rid="fig1_1">Figure 1.1A-D</xref>&#x2013;<xref ref-type="fig" rid="fig1_5">Figure 1.5C</xref>
        s/b
        <xref ref-type="fig" rid="fig1_1 fig1_2 fig1_3 fig1_4 fig1_5">Figure 1.1A-D&#x2013;Figure 1.5C</xref>
        
        
        <xref ref-type="table" rid="apptab1">Appendix Tables 1(a)</xref>&#x2013;<xref ref-type="table" rid="apptab12">12(c,d)</xref>
        s/b
        <xref ref-type="table" rid="apptab1 apptab2 apptab3 apptab4 apptab5 apptab6 apptab7 apptab8 apptab9 apptab10 apptab11 apptab12">Appendix Tables 1(a)&#x2013;12(c,d)</xref>
        
        <xref ref-type="fn" rid="fn1_1"><sup>1</sup></xref>&#x2013;<xref ref-type="fn" rid="fn1_5"><sup>5</sup></xref>
        s/b
        <xref ref-type="fn" rid="fn1_1 fn1_2 fn1_3 fn1_4 fn1_5"><sup>1&#x2013;5</sup></xref>
        
        <xref ref-type="fn" rid="fn1_10">10</xref>&#x2013;<xref ref-type="fn" rid="fn1_15">15</xref>
        s/b
        <xref ref-type="fn" rid="fn1_10 fn1_11 fn1_12 fn1_13 fn1_14 fn1_15">10&#x2013;15</xref>
        
        <xref ref-type="math" rid="eqn1_1">Eqns. 1.1</xref>&#x2013;<xref ref-type="math" rid="eqn1_5">1.5</xref>
        s/b
        <xref ref-type="math" rid="eqn1_1 eqn1_2 eqn1_3 eqn1_4 eqn1_5">Eqns. 1.1&#x2013;1.5</xref>
        
        <xref ref-type="book-part" rid="chapter9">Chapters 9</xref>&#x2013;<xref ref-type="book-part" rid="chapter11">11</xref>
        s/b
        <xref ref-type="book-part" rid="chapter9 chapter10 chapter11">Chapters 9&#x2013;11</xref>
        
        <xref ref-type="boxed-text" rid="box1_1">Box 1.1</xref> through <xref ref-type="boxed-text" rid="box1_3">1.3</xref>
        s/b
        <xref ref-type="boxed-text" rid="box1_1 box1_2 box1_3">Box 1.1 through 1.3</xref>
        
        -----------------------------
        <xref ref-type="fig" rid="fig2.5c">Figs. 2.5c</xref>-<xref ref-type="fig" rid="fig2.5g">g</xref>
        s/b
        <xref ref-type="fig" rid="fig2.5c fig2.5d fig2.5e fig2.5f fig2.5g">Figs. 2.5c-g</xref>
        -----------------------------
        <xref ref-type="book-part" rid="chapterII">Chapter II</xref> to <xref ref-type="book-part" rid="chapterV">V</xref>
        s/b
        <xref ref-type="book-part" rid="chapterII chapterIII chapterIV chapterV">Chapters II to V</xref>
        -----------------------------
        
        The script contains still four commented lines beginning with UltraEdit.outputWindow.write used by me for debugging purposes.
        expand_rid_in_xml_v2.zip (2.12 KiB)   0
        Second version of the script for expanding references with the necessary fixes and enhancements
        Best regards from an UC/UE/UES for Windows user from Austria

        62
        Advanced UserAdvanced User
        62

          5:24 - Aug 17#4

          The script works very well with small files but has a big problem with large files, most of my files are over 2.5 MB in size.

          In some cases this script is not working properly. Below is an example:

          Code: Select all

          -----------------------------
          <xref ref-type="fig" rid="fig2.5A">Figs. 2.5(A)</xref>-<xref ref-type="fig" rid="fig2.5D">(D)</xref>
          s/b
          <xref ref-type="fig" rid="fig2.5A fig2.5B fig2.5C fig2.5D">Figs. 2.5(A)-(D)</xref>
          -----------------------------
          <xref ref-type="book-part" rid="appi">App. i</xref> to <xref ref-type="book-part" rid="appv">v</xref>
          s/b
          <xref ref-type="book-part" rid="appi appii appiii appiv appv">App. i to v</xref>
          -----------------------------
          <xref ref-type="book-part" rid="part9">Parts 9</xref>&#x2013;<xref ref-type="book-part" rid="part11">11</xref>
          s/b
          <xref ref-type="book-part" rid="part9 part10 part11">Parts 9&#x2013;11</xref>
          -----------
          When extracting the numeric part of the RID it is not necessary to extract the prefix part of the RID, an update is needed for both upper/lower to work for Roman and Alphabet.

          6,675585
          Grand MasterGrand Master
          6,675585

            7:59 - Aug 17#5

            New variants like app and part or Roman numerals in lower case or figures with an upper case letter require of course further enhancements of the script to support these variants too. I am wondering myself why you could not make these enhancements by yourself as that was easy to do.

            I ran the script with all the examples copied together into a single file with replicating the examples until the file size increased to 3.010.689 bytes with 55243 lines. UltraEdit v2024.0.0.35 produced with the script in two seconds the resulting file with 2.880.954 bytes. App. i to v becomes Apps. i to v which I changed already in the input file too. I used next a Perl regular expression find with search expression ^(<xref.*)$\r\ns/b\r\n(?!\1).*\r\n for verification if all the modifications were done correct by the script in memory of built-in JavaScript engine.

            The attached ZIP file contains the once more enhanced script file and the input and output files used for testing the script. The enhancements can be seen easily by comparing with command Compare… in UltraEdit using UltraCompare Lite / Professional the opened script files expand_rid_in_xml_v2.js and expand_rid_in_xml_v3.js.
            expand_rid_in_xml_v3.zip (40.66 KiB)   0
            Third version of the script for expanding references with additional enhancements and the input and output files
            Best regards from an UC/UE/UES for Windows user from Austria