title: split a string based on a mapping - xslt
body: <p>As suggested. I am breaking the question <a href="https://stackoverflow.com/questions/12004513/bit-manipulation-and-string-message-structure-with-xslt">here</a> 
  , into parts.</p>
  
  <p>My input xml, indicates the presence of fields in a string. The input xml can have maximum 64 field elements. The input xml field elements always occur in ascending order.
  my input xml</p>
  
  <pre><code>&lt;Root&gt;
    &lt;element&gt;field2&lt;/element&gt;
    &lt;element&gt;field3&lt;/element&gt;
    &lt;element&gt;field21&lt;/element&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The string is defined as a variable in the xslt.</p>
  
  <p>my variable </p>
  
  <pre><code>&lt;xsl:variable name="inputstring" select="'013112316145ABC0812345678'"/&gt;
  </code></pre>
  
  <p>The input xml says that field 2, 3 and 21 are the only fields in the string, to be extracted based on a mapping xml</p>
  
  <p>Here is the mapping xml</p>
  
  <pre><code>&lt;Root&gt;
    &lt;field no="2" charlength="2"&gt;variable&lt;/field&gt;
    &lt;field no="3" total="4"&gt;fixed&lt;/field&gt;
    &lt;field no="21" charlength="2"&gt;
      &lt;subfield no="1" total="3"&gt;fixed&lt;/subfield&gt;
      &lt;subfield no="2" charlength="2" idcode="ABC"&gt;variable&lt;/subfield&gt;
    &lt;/field&gt;
    &lt;field no="63" charlength="2"&gt;
      &lt;format1&gt;
        &lt;subfield no="1" total="3"&gt;fixed&lt;/subfield&gt;
      &lt;/format1&gt;
      &lt;format2&gt;
        &lt;subfield no="1" total="3"&gt;fixed&lt;/subfield&gt;
        &lt;subfield no="2" total="7"&gt;fixed&lt;/subfield&gt;
      &lt;/format2&gt;
      &lt;format3&gt;
        &lt;subfield no="1" total="3"&gt;fixed&lt;/subfield&gt;
        &lt;subfield no="2" total="7"&gt;fixed&lt;/subfield&gt;
        &lt;subfield no="3" total="6"&gt;fixed&lt;/subfield&gt;
      &lt;/format3&gt;
    &lt;/field&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>The mapping xml tells the following</p>
  
  <ol>
  <li>There are four types of fields, fixed, variable, field having subfields(with fixed and variable) and field having subfields(with different fomats)</li>
  <li>Field number 2 is a variable field(as above), and the first two characters(charlength attribute) indicate the length of the field</li>
  <li>Field 3 is a fixed one, with a total of 4 characters.</li>
  <li>Field 21 is a field having subfields(with fixed and variable), where the first two chars(charlength) indicates the number of chars of the Field
  
  <ul>
  <li>All fixed ones(subfields) occur first, followed by the variable subfields</li>
  <li>The subfields in this, always starts with the idcode(for 21's sub, it is ABC), followed by the length of characters(charlength attribute), then the subfield. the length of chars can be 0 as well</li>
  <li>All fixed and variable fields occur, the length of 0 indicates absence of a subfield(above point)</li>
  </ul></li>
  <li>Field 63 is a field having subfields(with different fomats), depending on the length of the field(charlength attribute), different formats are possible
  
  <ul>
  <li>For field 63, if the length is 03(first two chars, charlenghth attribute), it is format 1. If 10, format 2, if it is 16, then format3</li>
  </ul></li>
  </ol>
  
  <p>My desired output xml</p>
  
  <pre><code>&lt;Root&gt;
    &lt;field2&gt;3&lt;/field2&gt;
    &lt;!--value is 3 as the charlength is 2(which is 01)--&gt;
    &lt;field3&gt;1123&lt;/field3&gt;
    &lt;!--field3 value is 1123 as it is fixed, total length of 4--&gt;
    &lt;field21&gt;
      &lt;subfield1&gt;145&lt;/subfield1&gt;
      &lt;!--subfield1 should be 145 as it is fixed length of total 3 chars--&gt;
      &lt;subfield2&gt;12345678&lt;/subfield2&gt;
      &lt;!--sufield2 starts with 'ABC', has length 08 chars--&gt;
    &lt;/field21&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <hr>
  
  <p>Edit by Sean.</p>
  
  <h2>Break-down</h2>
  
  <p>Here is a break-down of the mapping between input and output.</p>
  
  <p>This is a picture of our string variable $inputstring</p>
  
  <pre><code>'013112316145ABC0812345678'
  </code></pre>
  
  <p>This is broken up into 3 fields according to the field definitions...</p>
  
  <pre><code>013    -      1123  -  16145ABC0812345678
   |              |              v  
   v              v           field 21
  field2        field3  
  </code></pre>
  
  <p>Let's break-down field 2:</p>
  
  <pre><code> 01    3
    |    v
    |   payload for field 2. This is output
    v
  Contains the length(1) of the payload, which in this case is '01' = 1
  This length of this 'header' is given by mapping Root/field[@no="2"]/@charlength
  The "2" in this expression comes from the input document node at Root/element .
  </code></pre>
  
  <p>Lets break-down field 21:</p>
  
  <pre><code>16   145   ABC0812345678
   |    |       v
   |    |     subfield 2
   |    \ subfield 1
    \
     v
     Header for field 2. Says that the total field 2 length (header + subfield 1 +
  subfield 2 consists of 16 characters. The length for this header was derived from
  the mapping node at Root/field[@no="21"]/@charlength .
  </code></pre>
  
  <p>And for the final example: a break-down of field 21/ subfield 2. This is a picture of subfield 2</p>
  
  <pre><code>ABC   08   12345678
   |     |     |
   |     |     v
   |     |    This is the payload. It is output as the text node child of output
   |     |      subfield 2
   |     v
   v    Length of the following payload
   Signature. The length and value is equal to the mapping node
     Root/field[@no="21"]/subfield[@no="2"]/@idcode
  </code></pre>