tags:

views:

71

answers:

2

I have a CSV file, and a XSD from which I need to create an XML file which conforms to the XSD Schema. Is there a way to create an XML according to an XSD? Doing it manually seems unreasonable.

+4  A: 

XML::Compile can do that for you. XML::Compile::Schema has the bits probably most relevant to your problem.

rafl
Thanks, will check them later when I'll get home
soulSurfer2010
+1  A: 

If you're looking for some magic csv2xsd script, you're going to be disappointed. The problem is that CSV is a simple flat structure, while the XML is a hierarchical structure that is further constrained by the XSD. You will have to manually determine which fields of the CSV pertain to which elements in the XSD, and figure out how to deal with minimums, maximums, required elements, etc.

Using a tool that attempts to interpret, compile, or otherwise 'automatically' deal with a schema is going to end up requiring the same level of effort (at least) to perform the mapping, not including the additional learning curve and complexity you'd be introducing. For a one-time task, this won't be worth your while.

Honestly, the simplest thing is to understand the schema and generate your XML accordingly. If you need help with reading XSD, there are many tools that can help you visualize it. Another option is to generate sample XML from the schema as a model.

Mark Thomas
my XSD is quite small, one page, not so complicated, so basically what you mean is to look at the XSD, understand the structure, and use a module to write the xml according to the schema?
soulSurfer2010
Yep. Using a builder like XML::LibXML or XML::API (http://search.cpan.org/~mlawren/XML-API-0.25/lib/XML/API.pm) can help with the generation of the XML.
Mark Thomas