title: Recursive XSLT transformation
body: <p>Yet again a problematic task. I have a not really nice xml. For example the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Values&gt;
  &lt;record name='svc_sig'&gt;
  &lt;record name="sig_in"&gt;
      &lt;array depth="1" name="rec_fields" type="record"&gt;
          &lt;record&gt;
              &lt;!-- Some irrelevant metadata information with value node name... --&gt;
              &lt;value name="field_name"&gt;docTest&lt;/value&gt;
              &lt;value name="field_type"&gt;record&lt;/value&gt;
              &lt;value name="field_dim"&gt;0&lt;/value&gt;
              &lt;array depth="1" name="rec_fields" type="record"&gt;
                  &lt;record javaclass="com.wm.util.Values"&gt;
                      &lt;!-- Some irrelevant metadata information with value node name... --&gt;
                      &lt;value name="field_name"&gt;doc.name&lt;/value&gt;
                      &lt;value name="field_type"&gt;string&lt;/value&gt;
                      &lt;value name="field_dim"&gt;0&lt;/value&gt;
                  &lt;/record&gt;
              &lt;/array&gt;
          &lt;/record&gt;
          &lt;record&gt;
              &lt;value name="field_name"&gt;docListTest&lt;/value&gt;
              &lt;value name="field_type"&gt;record&lt;/value&gt;
              &lt;value name="field_dim"&gt;1&lt;/value&gt;
              &lt;array depth="1" name="rec_fields" type="record"&gt;
                  &lt;record&gt;
                      &lt;value name="field_name"&gt;d0&lt;/value&gt;
                      &lt;value name="field_type"&gt;record&lt;/value&gt;
                      &lt;value name="field_dim"&gt;0&lt;/value&gt;
                      &lt;array depth="1" name="rec_fields" type="record"&gt;
                          &lt;record&gt;
                              &lt;value name="field_name"&gt;d0.name&lt;/value&gt;
                              &lt;value name="field_type"&gt;string&lt;/value&gt;
                              &lt;value name="field_dim"&gt;0&lt;/value&gt;
                          &lt;/record&gt;
                      &lt;/array&gt;
                  &lt;/record&gt;
              &lt;/array&gt;
          &lt;/record&gt;
          &lt;record&gt;
              &lt;value name="field_name"&gt;packages_should_work&lt;/value&gt;
              &lt;value name="field_type"&gt;recref&lt;/value&gt;
              &lt;value name="field_dim"&gt;0&lt;/value&gt;
              &lt;value name="rec_ref"&gt;data:packages&lt;/value&gt;
          &lt;/record&gt;
          &lt;record&gt;
              &lt;value name="field_name"&gt;packages_list_should_work&lt;/value&gt;
              &lt;value name="field_type"&gt;recref&lt;/value&gt;
              &lt;value name="field_dim"&gt;1&lt;/value&gt;
              &lt;value name="rec_ref"&gt;data:packages&lt;/value&gt;
          &lt;/record&gt;
      &lt;/array&gt;
  &lt;/record&gt;
  &lt;/record&gt;
  &lt;/Values&gt;
  </code></pre>
  
  <p>For simplicity I need to map this xml to a java class that's already given and I can'T change it. With that in mind I have to transform this xml into another with meaningful names. For example:</p>
  
  <pre><code>&lt;sig_in&gt;
     &lt;record&gt;
        &lt;field_name&gt;docTest&lt;/field_name&gt;
        &lt;field_type&gt;record&lt;/field_type&gt;
        &lt;field_dim&gt;0&lt;/field_dim&gt;
     &lt;/record&gt;
     &lt;record&gt;
        &lt;field_name&gt;docListTest&lt;/field_name&gt;
        &lt;field_type&gt;record&lt;/field_type&gt;
        &lt;field_dim&gt;1&lt;/field_dim&gt;
     &lt;/record&gt;
     &lt;record&gt;
        &lt;field_name&gt;packages_should_work&lt;/field_name&gt;
        &lt;field_type&gt;recref&lt;/field_type&gt;
        &lt;field_dim&gt;0&lt;/field_dim&gt;
     &lt;/record&gt;
     &lt;record&gt;
        &lt;field_name&gt;packages_list_should_work&lt;/field_name&gt;
        &lt;field_type&gt;recref&lt;/field_type&gt;
        &lt;field_dim&gt;1&lt;/field_dim&gt;
     &lt;/record&gt;
  &lt;/sig_in&gt;
  </code></pre>
  
  <p>So far I created something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
  &lt;xsl:output method="xml" indent="yes" encoding="UTF-8" /&gt;
      &lt;xsl:template match="/" name="service_signature"&gt;
          &lt;sig_in&gt;
              &lt;xsl:for-each select="Values/record[@name='svc_sig']/record[@name='sig_in']/array[@name]/record"&gt;
                  &lt;record&gt;
                      &lt;field_name&gt;&lt;xsl:value-of select="value[@name='field_name']/text()"/&gt;&lt;/field_name&gt;
                      &lt;field_type&gt;&lt;xsl:value-of select="value[@name='field_type']/text()"/&gt;&lt;/field_type&gt;
                      &lt;field_dim&gt;&lt;xsl:value-of select="value[@name='field_dim']/text()"/&gt;&lt;/field_dim&gt;
                  &lt;/record&gt;
              &lt;/xsl:for-each&gt;
          &lt;/sig_in&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>While it works for the main elements will not work for the nested records. I could create a xsl:for-each and iterate over each item if the type is record, but that wouldn't solve much; because it can be any deep. I know I should use recursion i just can't image how could I do it in this particular case.</p>
  
  <p>@Edit - Some correction to nested types:</p>
  
  <pre><code>&lt;record&gt;
    &lt;field_name&gt;docListTree&lt;/field_name&gt;
    &lt;field_type&gt;record&lt;/field_type&gt;
    &lt;field_dim&gt;1&lt;/field_dim&gt;
    &lt;record&gt;
      &lt;field_name&gt;d0&lt;/field_name&gt;
      &lt;field_type&gt;record&lt;/field_type&gt;
      &lt;field_dim&gt;0&lt;/field_dim&gt;
      &lt;record&gt;
        &lt;field_name&gt;d0.name&lt;/field_name&gt;
        &lt;field_type&gt;string&lt;/field_type&gt;
        &lt;field_dim&gt;0&lt;/field_dim&gt;
      &lt;/record&gt;
    &lt;/record&gt;
  &lt;/record&gt;
  </code></pre>
  
  <p>So as you can see where the original type is nested I need the same in the resulting xml also. Or in a flat, I'd need some unique identifier in the parenting and child nodes so I know which contains which. On any way I should not loose the structure.</p>
  
  <p>@Edit:
   - Really sorry I wanted to save space and I failed to show the complexity behind the walls. So every record contains value names nodes. Most of them only contains useless metadata information I don't need. Also there're two records, with @name {sig_in, sig_out}, from which I only need sig_in, with the field_name, field_type, field_dim informations, along with the nested records on the same way. I'll look over all recommended options and will try to modify them to match the needs tho.</p>
  
  <p>Thanks for every help!
   - Joe</p>