title: Flatten XML into simple list with XSLT
body: <p>I have an XML document full of ICD10 codes formatted like this:</p>
  
  <pre><code>&lt;ICD10CM.index&gt;
      &lt;version&gt;2019&lt;/version&gt;
      &lt;title&gt;ICD-10-CM INDEX TO DISEASES and INJURIES&lt;/title&gt;
      &lt;letter&gt;
          &lt;title&gt;A&lt;/title&gt;
          &lt;mainTerm&gt;
              &lt;title&gt;Abnormal, abnormality, abnormalities&lt;/title&gt;
              &lt;seeAlso&gt;Anomaly&lt;/seeAlso&gt;
              &lt;term level="1"&gt;
                  &lt;title&gt;chromosome, chromosomal&lt;/title&gt;
                  &lt;code&gt;Q99.9&lt;/code&gt;
                  &lt;term level="2"&gt;
                      &lt;title&gt;with more than three X chromosomes,  female&lt;/title&gt;
                      &lt;code&gt;Q97.1&lt;/code&gt;
                  &lt;/term&gt;
                  &lt;term level="2"&gt;
                      &lt;title&gt;analysis result&lt;/title&gt;
                      &lt;code&gt;R89.8&lt;/code&gt;
                      &lt;term level="3"&gt;
                          &lt;title&gt;bronchial washings&lt;/title&gt;
                          &lt;code&gt;R84.8&lt;/code&gt;
                      &lt;/term&gt;
                      &lt;term level="3"&gt;
                          &lt;title&gt;cerebrospinal fluid&lt;/title&gt;
                          &lt;code&gt;R83.8&lt;/code&gt;
                      &lt;/term&gt;
                  &lt;/term&gt;
              &lt;/term&gt;
          &lt;/mainTerm&gt;
      &lt;/letter&gt;
  &lt;/ICD10CM.index&gt;
  </code></pre>
  
  <p>I would like to end up with a collapsed list of items, but each item in the final list needs to reference its parent(s), potentially recursively. I would like the output to look something like this:</p>
  
  <pre><code>&lt;codes&gt;
      &lt;code&gt;
          &lt;id&gt;Q99.9&lt;/id&gt;
          &lt;description&gt;Chromosome, chromosomal&lt;/description&gt;
      &lt;/code&gt;
      &lt;code&gt;
          &lt;id&gt;Q97.1&lt;/id&gt;
          &lt;description&gt;Chromosome, chromosomal – with more than three X chromosomes, female&lt;/description&gt;
      &lt;/code&gt;
      &lt;code&gt;
          &lt;id&gt;R89.8&lt;/id&gt;
          &lt;description&gt;Chromosome, chromosomal – analysis result&lt;/description&gt;
      &lt;/code&gt;
      &lt;code&gt;
          &lt;id&gt;R84.8&lt;/id&gt;
          &lt;description&gt;Chromosome, chromosomal – analysis result – bronchial washings&lt;/description&gt;
      &lt;/code&gt;
      &lt;code&gt;
          &lt;id&gt;R83.8&lt;/id&gt;
          &lt;description&gt;Chromosome, chromosomal – analysis result – cerebrospinal fluid&lt;/description&gt;
      &lt;/code&gt;
  &lt;/codes&gt;
  </code></pre>
  
  <p>I'm fairly new to XSLT and have only built a handful of transforms, so huge bonus points for helping me understand the answer instead of just providing a code snippet that does the job.</p>
  
  <p>This <code>level="3"</code> example is the most deeply-nested sample I found in a quick search of the data but I'd prefer an example that works for any depth.</p>
  
  <p>Edit: Since folks have asked, I am using XSLT 2.0</p>