[xsl] Comparing node-sets or groups. Deep-equal with an exception?

Subject: [xsl] Comparing node-sets or groups. Deep-equal with an exception?
From: "Flanders, Charles E (US) charles.flanders@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 6 Nov 2019 14:49:50 -0000
I need help with an issue that I haven't come across before and I'm not even
sure that I have the subject correct. I am working with XSLT 2.0. Please no
3.0 solutions.

I have a long list of <mac-group-2lvl>  structures as below. Most are unique.
However in some cases, they are identical except for the "ref" attribute value
on the <teref>  element.

I need to walk down through these <mac-group-2lvl>  node-sets or groups,
determine if they are identical, except for the aforementioned "ref" attribute
on <teref>. If they are identical, I need to move or copy the <teref>  element
to the first node-set, then delete the second and any succeeding "duplicate"
sets. The "duplicate" sets will always be immediately following siblings.

Input:

<mac>
<mac-group-2lvl>
              <groupno>011201</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>Item Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.70</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools61"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
<mac-group-2lvl>
              <groupno>011201</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>Item Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.70</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools63"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
         <mac-group-2lvl>
              <groupno>011201</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>Item Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.70</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools16"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
<mac-group-2lvl>
              <groupno>0112</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>A Thing Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.60</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools61"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
</mac>

Needed output:

<mac>
<mac-group-2lvl>
              <groupno>011201</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>Item Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.70</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools61"/>
                           <teref refs="tools63"/>
                           <teref refs="tools16"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
  	<mac-group-2lvl>
              <groupno>0112</groupno>
              <compassemgroup-2lvl>
                  <compassem>
                       <name>A Thing Name</name>
                  </compassem>
                  <qualify-2lvl>
                       <maintfunc func="replace"/>
                       <maintclass-2lvl>
                           <f>0.60</f>
                       </maintclass-2lvl>
                       <terefs>
                           <teref refs="tools61"/>
                       </terefs>
                  </qualify-2lvl>
              </compassemgroup-2lvl>
         </mac-group-2lvl>
</mac>

C Flanders                                                          

Current Thread