使用xsd将csv转换为xml [英] converting csv to xml with an xsd

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

问题描述

我想找到一种可重复使用的方式来获取CSV文件并从其生成符合指定XSD的XML文件。我没有真正找到一个可重用的方法。我使用了Altova MapForce,它允许我导入一个CSV文件和XSD,做映射比生成代码从这里,但代码需要重新生成每当XSD更改。 Altova还生产了大量的代码。



我的理想解决方案是一组Java类,我可以给一个CSV文件,一个XSD,并得到一个XML文件。我找不到这样的东西,我正在考虑可能创造的东西。



想法?有什么东西在这里使用XSLT基于

p>这似乎很容易做,但它不是。 XML Schema是一种文档验证语言,而不是文档生产语言。它不告诉你如何做一个新的文件;它会告诉您所做的文档是否有效。



例如,在XML Schema中创建一个由一系列可选选项组成的复杂类型是件微不足道的事情。 foo 元素可以有 bar baz 子,然后是 baz bat 子,然后是 foo bar bat 这使得可以确定这两个元素都有效的规则:

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

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

同时,这条规则给你几乎零帮助决定如何取一个元组数据项并从中创建 foo 元素。



一般来说,当有人问这个问题时,在一个或两个模式,他们使用它定义一个相对简单的文档结构。看起来直观的是,应该很容易使用这些模式作为映射过程的输入。它可能是。不容易,甚至可能的是一个映射过程,可以将任何模式作为输入。



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

 < item name =footype =stringsize =10allowNulls =true... /> 
< item name =bartype =dateallowNulls =false... />

然后我可以使用该元数据来控制CSV输入中的XML生成, 使用它来产生一个模式,我的程序产生的XML将符合。如果我更改了元数据,我的XML和模式会适当地更改。



当然,如果模式真的是一个输入到你的过程(例如,党),这甚至不会开始帮助你。


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.

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?

Thanks.

解决方案

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.

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>

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.

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" .../>

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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