I have a project where I need to convert a XML file to CSV file and vice versa. I can't use a single XSL file because, there are different XML and CSV formats. So, I am just wondering if there is any way (any tool or editors or any APIs) to create XSL file based on XSD file.
I am also open to any other suggestions too (I believe I can't avoid XSL because in the future I might be asked to convert to different formats such as pdf, html etc)
Like Jim Garrison said, there are no simple mapping between XSD and XSLT. But they are both XML formats, so it is not impossible to map one to the other. Depending on the general structure of the formats, it could also be possible to write a generic XSLT to map any XML directly to CSV, if the structure is simple enough.
You will have to provide some examples of the XML data, the XSD and expected result.
I realise that this was asked and answered a good 3 years ago, but I came across it while asking myself the same question. The short answer is yes, of course, because you are simply converting one type of XML to another (albeit with some structural and syntax changes). I saw this: https://www.oxygenxml.com/archives/xsl-list/200807/msg00601.html - which outlines a basic implementation as proof of concept, and I used that as a starting point to create the following XSLT:
Note the use of
xsl:element
to create the XSLT tags and creatingselect
andmatch
attributes, the quoting in selects and escaping. Thecomment
blocks are there to visually break up the root of the document (make it more readable) but serve no other purpose. Also, this requires an XSLT 2.0 processor.xsltproc
users need not apply.As per the previous responses, you'll have to modify it in varying degrees for your use case. I made this so that I could quickly create an accurate skeleton from which I could build out a useful XSLT document, while automating the tedious groundwork.
Naturally, I've just spent hours developing and testing something that at this point I probably could have done faster by hand with grep, but at least it was interesting. Hope this helps someone and improvements are welcome.
Answer is - YES. It can be done. If you look at Eclipse there is a function which generates sample XML based on XSD. So it will be possible to make own XSTL transformation for XSD to obtain another XSLT - but this time based on source XSD. I won't be easy but it is achievable.
Contrary to the assertion in your question, you can actually use a single XSLT file to convert from generic CSV to XML. Refer: Kernow's converter
An XSD file describes the structure of a valid XML file that conforms to certain rules. An XSLT file describes how to transform an input XML document to some output form, which may or may not be XML. It is not possible to deduce an XSL transformation from an XSD file as they address completely different aspects of XML.
In other words, an XSD allows you to confirm that an XML document adheres to a predefined set of constraints, but says nothing about what to do with the XML, or how to transform it to something else.