As suggested. I am breaking the question here , into parts.
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
<Root>
<element>field2</element>
<element>field3</element>
<element>field21</element>
</Root>
The string is defined as a variable in the xslt.
my variable
<xsl:variable name="inputstring" select="'013112316145ABC0812345678'"/>
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
Here is the mapping xml
<Root>
<field no="2" charlength="2">variable</field>
<field no="3" total="4">fixed</field>
<field no="21" charlength="2">
<subfield no="1" total="3">fixed</subfield>
<subfield no="2" charlength="2" idcode="ABC">variable</subfield>
</field>
<field no="63" charlength="2">
<format1>
<subfield no="1" total="3">fixed</subfield>
</format1>
<format2>
<subfield no="1" total="3">fixed</subfield>
<subfield no="2" total="7">fixed</subfield>
</format2>
<format3>
<subfield no="1" total="3">fixed</subfield>
<subfield no="2" total="7">fixed</subfield>
<subfield no="3" total="6">fixed</subfield>
</format3>
</field>
</Root>
The mapping xml tells the following
- There are four types of fields, fixed, variable, field having subfields(with fixed and variable) and field having subfields(with different fomats)
- Field number 2 is a variable field(as above), and the first two characters(charlength attribute) indicate the length of the field
- Field 3 is a fixed one, with a total of 4 characters.
- 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
- All fixed ones(subfields) occur first, followed by the variable subfields
- 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
- All fixed and variable fields occur, the length of 0 indicates absence of a subfield(above point)
- Field 63 is a field having subfields(with different fomats), depending on the length of the field(charlength attribute), different formats are possible
- 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
My desired output xml
<Root>
<field2>3</field2>
<!--value is 3 as the charlength is 2(which is 01)-->
<field3>1123</field3>
<!--field3 value is 1123 as it is fixed, total length of 4-->
<field21>
<subfield1>145</subfield1>
<!--subfield1 should be 145 as it is fixed length of total 3 chars-->
<subfield2>12345678</subfield2>
<!--sufield2 starts with 'ABC', has length 08 chars-->
</field21>
</Root>
Edit by Sean.
Break-down
Here is a break-down of the mapping between input and output.
This is a picture of our string variable $inputstring
'013112316145ABC0812345678'
This is broken up into 3 fields according to the field definitions...
013 - 1123 - 16145ABC0812345678
| | v
v v field 21
field2 field3
Let's break-down field 2:
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 .
Lets break-down field 21:
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 .
And for the final example: a break-down of field 21/ subfield 2. This is a picture of subfield 2
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