I have an XML document full of ICD10 codes formatted like this:
<ICD10CM.index>
<version>2019</version>
<title>ICD-10-CM INDEX TO DISEASES and INJURIES</title>
<letter>
<title>A</title>
<mainTerm>
<title>Abnormal, abnormality, abnormalities</title>
<seeAlso>Anomaly</seeAlso>
<term level="1">
<title>chromosome, chromosomal</title>
<code>Q99.9</code>
<term level="2">
<title>with more than three X chromosomes, female</title>
<code>Q97.1</code>
</term>
<term level="2">
<title>analysis result</title>
<code>R89.8</code>
<term level="3">
<title>bronchial washings</title>
<code>R84.8</code>
</term>
<term level="3">
<title>cerebrospinal fluid</title>
<code>R83.8</code>
</term>
</term>
</term>
</mainTerm>
</letter>
</ICD10CM.index>
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:
<codes>
<code>
<id>Q99.9</id>
<description>Chromosome, chromosomal</description>
</code>
<code>
<id>Q97.1</id>
<description>Chromosome, chromosomal – with more than three X chromosomes, female</description>
</code>
<code>
<id>R89.8</id>
<description>Chromosome, chromosomal – analysis result</description>
</code>
<code>
<id>R84.8</id>
<description>Chromosome, chromosomal – analysis result – bronchial washings</description>
</code>
<code>
<id>R83.8</id>
<description>Chromosome, chromosomal – analysis result – cerebrospinal fluid</description>
</code>
</codes>
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.
This level="3" 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.
Edit: Since folks have asked, I am using XSLT 2.0