使用 xsd 将 csv 转换为 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1674902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting csv to xml with an xsd
提问by Casey
I am trying to find a reusable way of taking a CSV file and generating an XML file from it that conforms to a specified XSD. I haven't really found a reusable approach for this. I have used Altova MapForce which lets me import a CSV file and XSD, do the mapping than generate code from this, but the code needs to be regenerated whenever the XSD changes. Altova also produces a lot of code.
我试图找到一种可重用的方式来获取 CSV 文件并从中生成符合指定 XSD 的 XML 文件。我还没有真正找到可重用的方法。我使用了 Altova MapForce,它允许我导入 CSV 文件和 XSD,进行映射而不是从中生成代码,但是每当 XSD 更改时都需要重新生成代码。Altova 还生成了大量代码。
My ideal solution would be a set of Java classes that I can give a CSV file to, an XSD and get an XML file out of it. I can't find anything like this though and I'm thinking about potentially creating something.
我的理想解决方案是一组 Java 类,我可以将它们提供给一个 CSV 文件、一个 XSD 并从中获取一个 XML 文件。不过我找不到这样的东西,我正在考虑可能创造一些东西。
Ideas? Is there something here using XSLT based on thisquestion?
想法?这里有没有基于这个问题使用 XSLT 的东西?
Thanks.
谢谢。
回答by Robert Rossney
This seems like something that would be easy to do, but it's not. XML Schema is a document validation language, not a document production language. It doesn't tell you how to make a new document; it tells you whether or not the document that you made is valid. Those aren't the same thing by a long shot.
这似乎很容易做到,但事实并非如此。XML Schema 是一种文档验证语言,而不是一种文档生产语言。它不会告诉您如何制作新文档;它会告诉您您制作的文件是否有效。从长远来看,这些不是一回事。
For instance, it's trivial to create a complex type in XML Schema that consists of a sequence of optional choices. A fooelement can have either a baror bazchild, then either a bazor batchild, then a foo, bar, or batchild. That makes for a rule that can determine that both of these elements are valid:
例如,在 XML Schema 中创建一个由一系列可选选项组成的复杂类型是微不足道的。一个foo元素可以有一个bar或baz孩子,然后是一个baz或bat孩子,那么foo,bar或bat孩子。这就形成了一个可以确定这两个元素都有效的规则:
<foo>
<baz/>
<baz/>
<bar/>
</foo>
<foo>
<foo>
<bar/>
</foo>
</foo>
At the same time, that rule gives you pretty much zero help in determining how to take a tuple of data items and create a fooelement from it.
同时,该规则在确定如何获取数据项元组并foo从中创建元素方面几乎没有帮助。
Generally, when someone asks this question, they're looking at one or two schemas they're using which define a relatively simple document structure. It seems intuitive that it should be easy to use those schemas as input to a mapping process. It probably is. What's not easy, or even possible, is a mapping process that can take anyschema as an input.
通常,当有人问这个问题时,他们会查看他们正在使用的一两个模式,这些模式定义了一个相对简单的文档结构。使用这些模式作为映射过程的输入应该很容易,这似乎很直观。大概是这样。可将任何模式作为输入的映射过程并不容易,甚至是不可能的。
What I've done instead, in my projects, is to simplify the problem. I've built programs that use CSV and XML and and support schema validation, but in these programs, the schema is an output. I've defined a simple XML metadata format, e.g.:
相反,我在我的项目中所做的是简化问题。我已经构建了使用 CSV 和 XML 并支持模式验证的程序,但在这些程序中,模式是一个输出. 我定义了一个简单的 XML 元数据格式,例如:
<item name="foo" type="string" size="10" allowNulls="true" .../>
<item name="bar" type="date" allowNulls="false" .../>
Then I can use that metadata to control XML production from CSV input, and I can alsouse it to produce a schema that the XML my program produces will conform to. If I change my metadata, my XML and schema changes appropriately.
然后我可以使用该元数据来控制来自 CSV 输入的 XML 生成,我还可以使用它来生成我的程序生成的 XML 将符合的模式。如果我更改元数据,我的 XML 和架构也会相应更改。
Of course, if the schemas are genuinely an input to your process (e.g. they're provided by a third party), this won't even start to help you.
当然,如果模式真的是您流程的输入(例如,它们由第三方提供),这甚至不会开始帮助您。
回答by marc_s
Well, I don't really have a ready-made, out-of-the-box solution for this, but maybe:
好吧,我真的没有现成的、开箱即用的解决方案,但也许:
read your CSV file with a library like FileHelphers; for this, you need to create a class
MyDataTypewhich describes the columns in the CSV, and you get an array ofMyDataTypeif you decorate that class with the proper XML serialization attributes like
[XmlIgnore],[XmlAttribute]and so forth, you might be able to just simply serialize out the resulting array ofMyDataTypeinto an XML that conforms to your XML schemaor if that doesn't work, you could create another class that maps to your XML requirements (generate it from the XSD you have), and just simply define a mapping between the two types
MyDataType(from your CSV) andMyXmlDataType(for your XML) with something like AutoMapper
使用FileHelphers 之类的库读取您的 CSV 文件;为此,您需要创建一个类
MyDataType来描述 CSV 中的列,并获得一个数组MyDataType如果您使用适当的 XML 序列化属性(例如
[XmlIgnore],[XmlAttribute]等等)装饰该类,您可能只需将结果数组序列化为MyDataType符合您的 XML 模式的 XML或者如果这不起作用,您可以创建另一个映射到您的 XML 要求的类(从您拥有的 XSD 生成它),并且只需简单地定义两种类型
MyDataType(来自您的 CSV)和MyXmlDataType(对于您的 XML)之间的映射使用AutoMapper 之类的东西
It's not boiler-plate - but fairly close, and you could possibly make that pretty much a "framework" to just simply plug in your own types (if you need to do this frequently).
它不是样板——但相当接近,你可以把它变成一个“框架”,只需简单地插入你自己的类型(如果你需要经常这样做)。
回答by Sean B. Durkin
If your XSLT engine is compliant with XSLT version 2, then the best solution is here:
如果您的 XSLT 引擎符合 XSLT 版本 2,那么最好的解决方案是:
回答by Jim Garrison
What you have is a single "table" (the CSV file) which contains (probably) denormalized rows representing a (probably) hierarchical data model. You want to map that to an arbitrary hierarchical XML document based on the XSD.
您拥有的是一个“表”(CSV 文件),其中包含(可能)表示(可能)分层数据模型的非规范化行。您希望将其映射到基于 XSD 的任意分层 XML 文档。
You'll need a tool that can map grouping key columns to XML elements and specify which data columns go in which attributes/child elements. This is a fairly significant problem, unless your mappings are trivial.
您需要一个工具来将分组键列映射到 XML 元素,并指定哪些数据列包含在哪些属性/子元素中。这是一个相当重要的问题,除非您的映射是微不足道的。
Could you post some samples of the CSV and XSD? That might help get a more focused answer.
你能发布一些 CSV 和 XSD 的样本吗?这可能有助于获得更集中的答案。
回答by Rashelinu
Microsoft Excel is able to export XML: http://office.microsoft.com/en-us/excel-help/export-xml-data-HP010206401.aspx
Microsoft Excel 能够导出 XML:http: //office.microsoft.com/en-us/excel-help/export-xml-data-HP010206401.aspx
I had some problems with creating an exportable XSD format, but this is a really great tool once you've got it working.
我在创建可导出的 XSD 格式时遇到了一些问题,但是一旦您开始使用它,这将是一个非常棒的工具。

