user3724711
user3724711

Reputation: 129

How to use xslt 1.0 to covert a fixed length text file to a XML file which separated using the offset and field length,

I was wondering if it is possible using XSL to convert a fixed length text file to an XML file.

Specifically for the below example.

I have a text file which looks like that: I used . to represent space for showing purpose

.....1.....0.....0.....0.......0.......0.......0
.....2.....1.....0.....0.......0.......0.......0
.....3.....1.....2.....3.......0.......0.......0
.....4.....1.....2.....3.......1.......2.......3

fields are separated using the offset and field length, my presumptions:

#MID#    offset = 0  length = 6 
#TID1#   offset = 7  length = 6 
#TID2#   offset = 13  length = 6 
#TID3#   offset = 19  length = 6 
#TITLE1# offset = 25  length = 8 
#TITLE2# offset = 33  length = 8 
#TITLE3# offset = 41  length = 8 

the data table will be looks like :

 ##MID##TID1##TID2##TID3##TITLE1##TITLE2##TITLE3#
 #   1#    0#    0#    0#      0#      0#      0#
 #   2#    1#    0#    0#      0#      0#      0#
 #   3#    1#    2#    3#      0#      0#      0#
 #   4#    1#    2#    3#      1#      2#      3#
 ################################################

I would like to convert the incoming text file into an XML file by using C#.net and XSLT (I guess there is no XSLT 2.0 processor offered by Microsoft, only XSLT 1.0 can be used in this case, Saxon 9.x and XQSharp just cost too much)

The target output XML would be like

<MID value='1'>
</MID>

<MID value='2'>
  <TID value='1'>
  </TID>
</MID>

<MID value='3'>
  <TID value='1'>
  </TID>
  <TID value='2'>
  </TID>
  <TID value='3'>
  </TID>
</MID>

<MID value='4'>
  <TID value='1'>
    <TID value='1'>
    </TID>
  </TID>
  <TID value='2'>
    <TID value='2'>
    </TID>
  </TID>
  <TID value='3'>
    <TID value='3'>
    </TID>
  </TID>
</MID>

Thanks a lot

Upvotes: 0

Views: 172

Answers (1)

wero
wero

Reputation: 33010

XSLT 1.0 needs a XML document as input.

Depending on the XSLT processor you still can feed non XML formats into the transformation if you have a parser which can read a file and present it as a XML: E.g. read CSV, JSON etc. and pass it to the XSLT engine as a series of parse events.

Your task is to read your CSV like input and turn it into XML. You could do this yourself, but then the task is solved and you don't need a XSLT step.

Upvotes: 1

Related Questions