Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
563 views
in Technique[技术] by (71.8m points)

xslt - converting csv to xml with an xsd

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...