本文介绍了使用 xsd 将 csv 转换为 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种可重用的方式来获取 CSV 文件并从中生成符合指定 XSD 的 XML 文件.我还没有真正找到可重用的方法.我使用了 Altova MapForce,它允许我导入 CSV 文件和 XSD,进行映射而不是从中生成代码,但是每当 XSD 更改时都需要重新生成代码.Altova 还生成了大量代码.

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.

我的理想解决方案是一组 Java 类,我可以将它们提供给 CSV 文件、XSD 并从中获取 XML 文件.不过我找不到这样的东西,我正在考虑可能创造一些东西.

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.

想法?这里有没有使用基于 这个问题?

Ideas? Is there something here using XSLT based on this question?

谢谢.

推荐答案

这似乎很容易做到,但事实并非如此.XML Schema 是一种文档验证语言,而不是一种文档生产语言.它不会告诉您如何制作新文档;它会告诉您您制作的文件是否有效.从长远来看,这些不是一回事.

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 中创建一个由一系列可选选项组成的复杂类型是微不足道的.foo 元素可以有 barbaz 子元素,然后是 bazbat 子,然后是 foobarbat 子.这就形成了一个可以确定这两个元素都有效的规则:

For instance, it's trivial to create a complex type in XML Schema that consists of a sequence of optional choices. A foo element can have either a bar or baz child, then either a baz or bat child, then a foo, bar, or bat child. That makes for a rule that can determine that both of these elements are valid:

<foo>
   <baz/>
   <baz/>
   <bar/>
</foo>

<foo>
   <foo>
      <bar/>
   </foo>
</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 foo element from it.

通常,当有人问这个问题时,他们会查看他们正在使用的一两个模式,这些模式定义了一个相对简单的文档结构.使用这些模式作为映射过程的输入应该很容易,这似乎很直观.大概是这样.可以将任何模式作为输入的映射过程并不容易,甚至是不可能的.

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 any schema as an input.

相反,在我的项目中,我所做的是简化问题.我已经构建了使用 CSV 和 XML 并支持模式验证的程序,但在这些程序中,模式是一个输出.我已经定义了一个简单的 XML 元数据格式,例如:

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.:

<item name="foo" type="string" size="10" allowNulls="true" .../>
<item name="bar" type="date" allowNulls="false" .../>

然后我可以使用该元数据来控制来自 CSV 输入的 XML 生成,并且我可以使用它来生成我的程序生成的 XML 将符合的模式.如果我更改元数据,我的 XML 和架构也会相应更改.

Then I can use that metadata to control XML production from CSV input, and I can also use 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.

当然,如果模式确实是您流程的输入(例如,它们由第三方提供),这甚至不会开始帮助您.

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.

这篇关于使用 xsd 将 csv 转换为 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:54
查看更多