从csv数据生成xml符合给定的xsd模式 [英] Generate xml from csv data in conformance with given xsd schema

查看:142
本文介绍了从csv数据生成xml符合给定的xsd模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml模式和csv数据来生成相应的xml文件。

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

我发现 ,如何从给定的CSV文件生成xml文件。但是对于我的模式,它总是相同的映射,因为元素。因此,最后5列是根据DataType列映射的。

I found this, how to generate xml files from given CSV file. But with my schema, it's always the same mapping because of the element. So the last 5 columns are mapped according to the DataType column.

我可以扩展代码与case开关,并在每种情况下相应地映射每个元素,但应该有一个更简单的方法来做到这一点。我是新的到xml,需要一些帮助在这里。

P.S:我尝试所有的工具,广告和免费,广告地图功能,没有发现任何东西。此外,Excel不使用非标准化模式。

I could expand the code with case switch, and map every element accordingly in each case, but there should be a simpler way to do this. I am new to xml, need some help here.
P.S: I tried all the tools, commercial and free, that advertise mapping capabilities, and found nothing to do this. Also Excel does not work with a denormalized schema.

任何帮助感谢,谢谢

Update1:​​工具:C#命令行代码应该是最简单的。
Update2:目标是使用给定csv文件中的数据生成具有指定模式的xml。

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

CSV数据 p>

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,,

架构定义

<?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>


推荐答案

快速解决方案)将:


  1. 使用 mysqlimport 将CSV拉入临时mysql

  2. 使用 mysqldump -X 将该表格输出为简单的XML档案
  3. 输出的XML与一个XSL样式表映射到您需要的模式。
  1. use mysqlimport to pull the CSV into a temporary mysql table
  2. use mysqldump -X to output that table as a simple XML file
  3. process the outputted XML with an XSL stylesheet to map to your required schema.

如果你这样做,会更好,但原则是一样的:

If you're doing this regularly then something more robust/scriptable would be better, but the principle is the same:

1)将CSV转换为与CSV格式相同的非常简单的XML

1) convert your CSV to very simple XML in the same format as the 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)通过XSL处理XML以获取按照模式格式化的XML文档。

2) process that XML through XSL to get an XML doc formatted following your schema.

这篇关于从csv数据生成xml符合给定的xsd模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆