Record-Separated Semi-Fixed-Length Fields To and From XML

Using "-1" as the length value of the last field in a record means that the field is of undefined length and that the end of the record is used to signal the end of the field.

Here are two transformation examples.

Record-Separated Semi-Fixed Length Fields to XML

Here is an example on how a record-separated semi-fixed length fields in a flat file is transformed to XML.

Flat File Definition:

<BankRepository>
	<Files>
		<File>
			<ID>SampleTwelve</ID>
			<Messages>
				<Message>
               <IgnorePrecedingFieldSeparator>1</IgnorePrecedingFieldSeparator>
               <DefaultEmptyFieldValue>
                  <Numeric>0</Numeric>
                  <Alpha></Alpha>
                </DefaultEmptyFieldValue>
                <DefaultAlignment>
                  <Numeric>RIGHT</Numeric>
                  <Alpha>LEFT</Alpha>
                </DefaultAlignment>
                <DefaultPadding>
                  <Numeric>0</Numeric>
                  <Alpha> </Alpha>
                </DefaultPadding>
                <DefaultTrim>
                  <Numeric>1</Numeric>
                  <Alpha>0</Alpha>
                </DefaultTrim>
					<Name>TopNode</Name>
					<TagName>TopNode</TagName>										
					<EscapeChar/>
					<RecordSeparator>CRLF</RecordSeparator>
					<FieldSeparator></FieldSeparator>
					<!--<FixedLengthSeparator>1</FixedLengthSeparator>-->
					<Records>
						<Record>
							<Name>RecordOne</Name>
							<TagName>RecordOne</TagName>
							<MinOccur>1</MinOccur>
							<MaxOccur>0</MaxOccur>
							<Fields>
								<Field>
									<Name>Field1</Name>
									<TagName>Field1</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>1</StartPos>
									<Length>3</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
								<Field>
									<Name>Field2</Name>
									<TagName>Field2</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>4</StartPos>
									<Length>3</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
								<Field>
									<Name>Field3</Name>
									<TagName>Field3</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>7</StartPos>
									<Length>-1</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
							</Fields>
						</Record>
					</Records>
				</Message>
			</Messages>
		</File>
	</Files>
</BankRepository>

The input with variable length last field is:

A B CDEFG
1 4 789

The expected result is:

<TopNode>
	<RecordOne>
		<Field1>A</Field1>
		<Field2>B</Field2>
		<Field3>CDEFG</Field3>
	</RecordOne>
	<RecordOne>
		<Field1>1</Field1>
		<Field2>4</Field2>
		<Field3>789</Field3>
	</RecordOne>
</TopNode>

XML to Record-Separated Semi-Fixed-Length Fields

This is the opposite of the previous example. The output and repositories in the previous example are used here to generate new sample output.

Flat File Definition:

<BankRepository>
	<Files>
		<File>
			<ID>SampleTwelve</ID>
			<Messages>
				<Message>
               <IgnorePrecedingFieldSeparator>1</IgnorePrecedingFieldSeparator>
               <DefaultEmptyFieldValue>
                  <Numeric>0</Numeric>
                  <Alpha></Alpha>
                </DefaultEmptyFieldValue>
                <DefaultAlignment>
                  <Numeric>RIGHT</Numeric>
                  <Alpha>LEFT</Alpha>
                </DefaultAlignment>
                <DefaultPadding>
                  <Numeric>0</Numeric>
                  <Alpha> </Alpha>
                </DefaultPadding>
                <DefaultTrim>
                  <Numeric>1</Numeric>
                  <Alpha>0</Alpha>
                </DefaultTrim>
					<Name>TopNode</Name>
					<TagName>TopNode</TagName>										
					<EscapeChar/>
					<RecordSeparator>CRLF</RecordSeparator>
					<FieldSeparator></FieldSeparator>
					<!--<FixedLengthSeparator>1</FixedLengthSeparator>-->
					<Records>
						<Record>
							<Name>RecordOne</Name>
							<TagName>RecordOne</TagName>
							<MinOccur>1</MinOccur>
							<MaxOccur>0</MaxOccur>
							<Fields>
								<Field>
									<Name>Field1</Name>
									<TagName>Field1</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>1</StartPos>
									<Length>3</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
								<Field>
									<Name>Field2</Name>
									<TagName>Field2</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>4</StartPos>
									<Length>3</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
								<Field>
									<Name>Field3</Name>
									<TagName>Field3</TagName>
									<DataType>Alpha</DataType>
									<PaddingChar/>
									<DecimalLength/>
									<Alignment>LEFT</Alignment>
									<StartPos>7</StartPos>
									<Length>-1</Length>
									<DefaultValue></DefaultValue>
									<Identifier>0</Identifier>
								</Field>
							</Fields>
						</Record>
					</Records>
				</Message>
			</Messages>
		</File>
	</Files>
</BankRepository>

Input file:

<TopNode>
	<RecordOne>
		<Field1>A</Field1>
		<Field2>B</Field2>
		<Field3>CDEFG</Field3>
	</RecordOne>
	<RecordOne>
		<Field1>1</Field1>
		<Field2>4</Field2>
		<Field3>789</Field3>
	</RecordOne>
</TopNode>

The expected result is:

A B CDEFG
1 4 789