title: Restoring relative chain contained in multiple files
body: <p>This question is a logical continuation of the previous <a href="https://stackoverflow.com/questions/59447626/restore-and-explicate-child-parents-interconnection-hidden-in-attributes-data">question</a> (formally, only the names of elements are changed. Here, structurally, everything remains the same). The new condition is that now two additional hierarchy levels are added. Each new level is represented in a separate xml file. The overall aim remains the same: restore the entire chain of hierarchy from the lowest element to the highest within one file. Source data is contained in following files:</p>
  
  <ul>
  <li>1-base.xml       (contains elements region, city, street) </li>
  <li>2-houses.xml
  (contains element house)</li>
  <li>3.rooms.xml   (contains element room)</li>
  </ul>
  
  <p><strong>I - base.xml</strong> - solved part in the past. Restoring the hierarchy within one file was resolved in an earlier <a href="https://stackoverflow.com/questions/59447626/restore-and-explicate-child-parents-interconnection-hidden-in-attributes-data">question</a>. Hierarchy model is parent --> subparent --> child (here is represented by region --> city --> street).</p>
  
  <ul>
  <li>child's ID-2="value" is equal subparent ID-1="value"</li>
  <li>subparent ID-2="value" is equal parent's ID-1="value"</li>
  </ul>
  
  <p><strong>visualization scheme</strong> for base.xml <a href="https://imgur.com/WE0ociU" rel="nofollow noreferrer">here</a></p>
  
  <p>solution codes for base.xml:</p>
  
  <p>1-source:</p>
  
  <pre><code>&lt;document ID-1="regionID"   ID-2="NULL"  ID-3="value" attr-4="region"/&gt;      &lt;!-- this is parent's node --&gt;
  &lt;document ID-1="cityID" ID-2="regionID" ID-3="value" attr-4="city" /&gt;       &lt;!-- this is subparent --&gt;
  
  &lt;document ID-1="aaa" ID-2="cityID" ID-3="value" attr-4="street-1"/&gt;     &lt;!-- child-1 --&gt;
  &lt;document ID-1="bbb" ID-2="cityID" ID-3="value" attr-4="street-2"/&gt;     &lt;!-- child-2 --&gt;
  &lt;document ID-1="ccc" ID-2="cityID" ID-3="value" attr-4="street-3"/&gt;     &lt;!-- child-3 --&gt;
  &lt;document ID-1="ddd" ID-2="cityID" ID-3="value" attr-4="street-4"/&gt;     &lt;!-- child-4 --&gt;
  </code></pre>
  
  <p>2-xslt-solution</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      version="1.0"&gt;
  
  &lt;xsl:template match="@* | node()"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="@* | node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:key name="ref" match="document" use="@ID-1"/&gt;
  
  &lt;xsl:template match="document"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:copy-of select="@*"/&gt;
          &lt;xsl:apply-templates select="key('ref', @ID-2)" mode="att"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="document" mode="att"&gt;
      &lt;xsl:param name="pos" select="count(@*) + 1"/&gt;
      &lt;xsl:attribute name="attr-{$pos}"&gt;
          &lt;xsl:value-of select="@attr-4"/&gt;
      &lt;/xsl:attribute&gt;
      &lt;xsl:apply-templates select="key('ref', @ID-2)" mode="att"&gt;
          &lt;xsl:with-param name="pos" select="$pos + 1"/&gt;
      &lt;/xsl:apply-templates&gt;
  &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>3-output</p>
  
  <pre><code>&lt;document ID-1="regionID"   ID-2="NULL"  ID-3="value" attr-4="region"/&gt;                &lt;!-- this is parent's date --&gt;
  &lt;document ID-1="cityID" ID-2="regionID" ID-3="value" attr-4="city" attr-5="region"/&gt;   &lt;!-- this is subparent --&gt;
  
  &lt;document ID-1="aaa" ID-2="cityID" ID-3="value" attr-4="street-1"  attr-5="city" attr-6="region" /&gt;  &lt;!-- child-1 --&gt;
  &lt;document ID-1="bbb" ID-2="cityID" ID-3="value" attr-4="street-2"  attr-5="city" attr-6="region" /&gt;  &lt;!-- child-2 --&gt;
  &lt;document ID-1="ccc" ID-2="cityID" ID-3="value" attr-4="street-3"  attr-5="city" attr-6="region" /&gt;  &lt;!-- child-3 --&gt;
  &lt;document ID-1="ddd" ID-2="cityID" ID-3="value" attr-4="street-4"  attr-5="city" attr-6="region" /&gt;  &lt;!-- child-4 --&gt;
  </code></pre>
  
  <p><strong>II - houses.xml</strong>
  full new visualization scheme <a href="https://imgur.com/C2drnBg" rel="nofollow noreferrer">here</a></p>
  
  <p>Element “house” has an interconnecting attribute “ID-1”. It connects houses.xml with base.xml The following rule is true for this attribute: “Each ID-1’s from houses.xml has a connection with base.xml ID-1’s (street lvl). But not each ID-1 from base.xml connected with ID-1’s from houses.xml (not connected city and region levels).</p>
  
  <p><em>some thoughts:</em> in base.xml example restoring movement went from the higher level to the low. In this case, I can assume a variant that make computation in opposite way - from low to top. So maybe it could be useful to remind that direct following ID-1’s line may not match and restore all nodes (city and region). </p>
  
  <p><strong>III - rooms.xml</strong> - the lowest level file</p>
  
  <p>so, all source codes are:</p>
  
  <p><em>base.xml</em></p>
  
  <pre><code>&lt;document ID-1="regionID"   ID-2="NULL"  ID-3="value" attr-4="region"/&gt;      &lt;!-- this is parent's node --&gt;
  &lt;document ID-1="cityID" ID-2="regionID" ID-3="value" attr-4="city" /&gt;       &lt;!-- this is subparent --&gt;
  
  &lt;document ID-1="aaa" ID-2="cityID" ID-3="value" attr-4="street-1"/&gt;     &lt;!-- child-1 --&gt;
  &lt;document ID-1="bbb" ID-2="cityID" ID-3="value" attr-4="street-2"/&gt;     &lt;!-- child-2 --&gt;
  &lt;document ID-1="ccc" ID-2="cityID" ID-3="value" attr-4="street-3"/&gt;     &lt;!-- child-3 --&gt;
  &lt;document ID-1="ddd" ID-2="cityID" ID-3="value" attr-4="street-4"/&gt;     &lt;!-- child-4 --&gt;
  </code></pre>
  
  <p><em>houses.xml</em></p>
  
  <pre><code>&lt;houses&gt;
      &lt;house ID-1="aaa" HOUSE-ID="zzzz" housenum="17" buildnum="a"/&gt;
      &lt;house ID-1="bbb" HOUSE-ID="yyyy" housenum="18" buildnum="NULL"/&gt;
      &lt;house ID-1="ccc" HOUSE-ID="xxxx" housenum="19" buildnum="NULL"/&gt;
      &lt;house ID-1="ddd" HOUSE-ID="wwww" housenum="20" buildnum="NULL"/&gt;
  &lt;/houses&gt;
  </code></pre>
  
  <p><em>rooms.xml</em></p>
  
  <pre><code>&lt;rooms&gt;
      &lt;room ROOM-ID="value" HOUSE-ID="zzzz" roomnum="123" roomtype="value"/&gt;
      &lt;room ROOM-ID="value" HOUSE-ID="yyyy" roomnum="345" roomtype="value"/&gt;
      &lt;room ROOM-ID="value" HOUSE-ID="xxxx" roomnum="567" roomtype="value"/&gt;
      &lt;room ROOM-ID="value" HOUSE-ID="wwww" roomnum="789" roomtype="value"/&gt;
  &lt;/rooms&gt;
  </code></pre>
  
  <p><strong>approximate output.</strong> Here assumed output represented as a new .xml file, but it can be based on, for example, rooms.xml . In particilar solution it may differ,  the principle of reproduction of the full parental chain is important. Crossnamed interconnecting attributes (HOUSE-ID, ID-1 may come from different pathes, depending on soution's exact expression. Somehow they just have to stay. </p>
  
  <pre><code>&lt;Restored-Objects&gt;
      &lt;object ROOM-ID="value" HOUSE-ID="zzzz" roomnum="123" roomtype="value"                  &lt;!--room  attrributes --&gt;
              ID-1="aaa" housenum="17" buildnum="a"                                           &lt;!--house attrributes --&gt;
              ID-2="cityID" ID-3="value" attr-4="street-1"  attr-5="city" attr-6="region"     &lt;!--base  attrributes --&gt;
      /&gt;
      &lt;object ROOM-ID="value" HOUSE-ID="yyyy" roomnum="345" roomtype="value"
              ID-1="bbb" housenum="18" buildnum="NULL"
              ID-2="cityID" ID-3="value" attr-4="street-2"  attr-5="city" attr-6="region" 
      /&gt;
      &lt;object ROOM-ID="value" HOUSE-ID="xxxx" roomnum="567" roomtype="value"
              ID-1="ccc" housenum="19" buildnum="NULL"    
              ID-2="cityID" ID-3="value" attr-4="street-3"  attr-5="city" attr-6="region"
      /&gt;
      &lt;object ROOM-ID="value" HOUSE-ID="wwww" roomnum="789" roomtype="value" 
              ID-1="ddd" housenum="20" buildnum="NULL"
              ID-2="cityID" ID-3="value" attr-4="street-4"  attr-5="city" attr-6="region"
      /&gt;      
      &lt;object ID-1="regionID"   ID-2="NULL"  ID-3="value" attr-4="region"/&gt;                &lt;!-- this is parent's date --&gt;
      &lt;object ID-1="cityID" ID-2="regionID" ID-3="value" attr-4="city" attr-5="region"/&gt;   &lt;!-- this is subparent --&gt;
  &lt;/Restored-Objects&gt;
  </code></pre>
  
  <p>I will welcome any solution from XSLT 1.0 to 3.0. The third version is also interesting because the file sizes. If not cut, they are large. So a streamable version can come in handy.</p>