0

I'm trying to build a mapping logic for csv to xml conversion using xsl template. I am trying to map multiple values in each row to a group of repeatable elements in xml.

an example csv looks like this:

Field A Field B Field C Field D Field E Field F Field G Field H Field I FieldJ 
123 bravo   alpha   500 8.5 Portugal                
123 charlie alpha               100 2.5     
123 delta   alpha                       23  ECHO

When I do the conversion I would like to see it mapped as follows:

 <Field_A>
     <Field_B>
     <Field_D>500</Field_D>
     <Field_E>8.5</Field_E>
     <Field_F>Portugal</Field_F>
 </Field_B>
 <Field_B>
     <Field_G>100</Field_G>
     <Field_H>2.5</Field_H>
 <Field_B>
     <Field_I>23</Field_I>
     <Field_J>ECHO</Field_J>
 </Field_B>
</Field_A>

So for each value in Field B, where value in Filed A & C are the same, map the values from Field D, E , F, G, H,I,J to specific elements.

To be honest, I don't know where to start. I have an if statement first:

<Field_A>
   <xsl: if test="@Field_B = 'bravo'>
       <Field_D><xsl: value - of select = "Field_D"/></Field D>
       <Field_E><xsl: value - of select = "Field_E"/></Field_E>
  .....

ETC.

How do I insert a for each loop and how can I make sure the right values are mapped to the right tags?

Do I need another column in csv with true/ false indicator which rows will have to belong to the same parent element and then map the value to a specific tag (for multiple Field_B elements that will be coming in csv from multiple rows but belonging to the same parent element)

When I do the conversion I would like to see it mapped as follows:

 <Field_A>
    <Field_B>
        <Field_D>500</Field_D>
        <Field_E>8.5</Field_E>
        <Field_F>Portugal</Field_F>
    </Field_B>
    <Field_B>
        <Field_G>100</Field_G>
        <Field_H>2.5</Field_H>
    <Field_B>
        <Field_I>23</Field_I>
        <Field_J>ECHO</Field_J>
    </Field_B>
  </Field_A>

There can also be multiple Field_B elements with differenc values.

Miya
  • 1
  • 1
  • apologies the csv didn't seem to paste properly: – Miya Apr 09 '19 at 10:25
  • Typically, XSLT would be used to convert XML to another format (such as CSV), not the other way around. However, if you really wanted to convert CSV to XML then it is possible, although it would be done using text manipulation. If you can use XSLT 2.0, check out this question: https://stackoverflow.com/questions/29295377/xslt-2-0-to-convert-csv-to-xml-format – Tim C Apr 09 '19 at 10:37
  • Note that `` with a space in there is not even anything like XML so you first need to clear which XML format you want. – Martin Honnen Apr 09 '19 at 11:28
  • And what exactly is the separator character in your rows .e.g. in `Field A Field B Field C Field D Field E Field F Field G Field H Field I FieldJ` or `123 bravo alpha 500 8.5 Portugal`? I don't see any comma separated data and space separation might work for the data row but for the field names it is not clear. – Martin Honnen Apr 09 '19 at 11:49
  • Apologies I was mocking this up very quickly to get the context, there are no spaces in the xsl document, it was just for reference and visualisation. Comma delimited these values are in csv. – Miya Apr 09 '19 at 15:44
  • FieldA - FieldJ, or we can call them A to J if easier. – Miya Apr 09 '19 at 16:29

0 Answers0