Generate xml from csv data according to a given xsd schema

I have an xml schema and csv data to create the corresponding xml files.

I found this how to generate xml files from a given CSV file. But with my circuit, it is always the same mapping due to the element. Thus, the last 5 columns are displayed according to the DataType column.

I could extend the code with a case switch and map each element accordingly in each case, but there should be an easier way to do this. I'm new to xml, you need help here. PS: I tried all the tools, commercial and free, to advertise the matching capabilities and did not find anything for it. In addition, Excel does not work with a denormalized schema.

Any help appreciated, thanks Update1: Tools: C # command line code should be the easiest. Update2: The goal is to generate xml with the specified schema using data from the given csv file.

CSV data

EntityName,FieldName,SQLType,DataType,Nullable,Caption,ColumnIndex,MinStringLength,MaxStringLength,D_Precision,D_Scale
SOChemistryRequirement,CE_Min,"decimal(7, 5)",Decimal,TRUE,CE_Min,82,,,7,5
SOChemistryRequirement,CE_Max,"decimal(7, 5)",Decimal,TRUE,CE_Max,83,,,7,5
SOTestRequirement,Weldability,bit,bool,FALSE,Weldability,107,,,,
SONumber,SONumber,varchar(6),string,FALSE,SONumber,0,,6,,

circuit definition:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DataTypes" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- No empty string data type. -->
<xs:simpleType name="NoEmptyString">
  <xs:restriction base="xs:string">
  <xs:minLength value="1" />
</xs:restriction>
</xs:simpleType>

<!-- Root element. -->
<xs:element name="ExcelFile">
<xs:complexType>
  <xs:sequence>
    <xs:element name="SheetName" type="NoEmptyString" />
    <xs:element name="CellRange" type="NoEmptyString" />
    <!-- Definition of columns. -->
    <xs:element name="Columns">
      <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
          <!-- Information about one column. -->
          <xs:element name="Column">
            <xs:complexType>
              <xs:sequence>
                <!-- Column index in excel file. -->
                <xs:element name="ColumnIndex" type="xs:unsignedByte" />
                <!-- Caption for current column in grid in importing screen. -->
                <xs:element name="Caption" type="xs:string" />
                <!-- Data type of current column. -->
                <xs:element name="DataType">
                  <xs:complexType>
                    <!-- It can be only one from following: -->
                    <xs:choice>
                      <xs:element name="Boolean"/>
                      <xs:element name="Int16">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="MinValue" type="xs:short" minOccurs="0" />
                            <xs:element name="MaxValue" type="xs:short" minOccurs="0" />
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                      <xs:element name="Int32">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="MinValue" type="xs:int" minOccurs="0" />
                            <xs:element name="MaxValue" type="xs:int" minOccurs="0" />
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                      <xs:element name="Int64">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="MinValue" type="xs:long" minOccurs="0" />
                            <xs:element name="MaxValue" type="xs:long" minOccurs="0" />
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                      <xs:element name="Decimal">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="MinValue" type="xs:decimal" minOccurs="0" />
                            <xs:element name="MaxValue" type="xs:decimal" minOccurs="0" />
                            <xs:element name="Precision" type="xs:int" minOccurs="0" />
                            <xs:element name="Scale" type="xs:int" minOccurs="0" />
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                      <xs:element name="DateTime"/>
                      <xs:element name="String">
                        <xs:complexType>
                          <xs:sequence>
                            <xs:element name="MinLength" type="xs:int" minOccurs="0" />
                            <xs:element name="MaxLength" type="xs:int" minOccurs="0" />
                          </xs:sequence>
                        </xs:complexType>
                      </xs:element>
                      <xs:element name="Custom"/>
                    </xs:choice>
                    </xs:complexType>
                    </xs:element>
                    <!-- Can be NULL value? -->
                    <xs:element name="IsNullable" type="xs:boolean" />
                    <!-- Entity name. Cannot be NULL. -->
                    <xs:element name="EntityName" type="NoEmptyString" />
                    <!-- Field name. It can be NULL because of composite target field. -->
                    <xs:element name="FieldName" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
0
source share
1 answer

( ) :

  • mysqlimport, CSV mysql
  • mysqldump -X XML .
  • XML XSL .

, /, :

1) CSV XML , CSV:

<csv>
  <record>
    <EntityName>SOChemistryRequirement</EntityName>
    <FieldName>CE_Min</FieldName>
    <SQLType>"decimal(7, 5)"</SQLType>
    <DataType>Decimal</DataType>
    <Nullable>TRUE</Nullable>
    <Caption>CE_Min</Caption>
    <ColumnIndex>82</ColumnIndex>
    <MinStringLength></MinStringLength>
    <MaxStringLength></MaxStringLength>
    <D_Precision>7</D_Precision>
    <D_Scale>5</D_Scale>
  </record>
  <!-- etc... -->
</csv>

2) XML XSL, XML-, .

0

All Articles