Is there a way to dynamically create an XSL file based on an XSD file

I have a project where I need to convert an XML file to a CSV file and vice versa. I cannot use one XSL file because there are different XML and CSV formats. So I'm just wondering if there is any way (any tool or editors or any APIs) for creating an XSL file based on an XSD file.

I am also open to any other suggestions (I believe that I can not avoid XSL, because in the future I may be asked to convert to different formats, such as pdf, html, etc.)

+4
source share
4 answers

An XSD file describes the structure of a valid XML file that conforms to certain rules. An XSLT file describes how to convert an input XML document to some output form, which may or may not be XML. It is not possible to derive an XSL transform from an XSD file because they affect completely different aspects of XML.

In other words, XSD allows you to confirm that the XML document adheres to a predefined set of restrictions, but does not say anything about what to do with XML, or how to convert it to something else.

+2
source

I understand that this was asked and answered quite well 3 years ago, but I stumbled upon it, 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 syntactic changes). I saw this: https://www.oxygenxml.com/archives/xsl-list/200807/msg00601.html - which presents the main implementation as a proof of concept, and I used this as a starting point to create the next XSLT

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" method="xml" /> <xsl:template match="/"> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:element name="xsl:stylesheet"> <xsl:namespace name="xsl" select="'http://www.w3.org/1999/XSL/Transform'" /> <xsl:attribute name="version" select="'1.0'" /> <xsl:element name="xsl:output"> <xsl:attribute name="indent" select="'yes'" /> <xsl:attribute name="method" select="'xml'" /> </xsl:element> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:comment> </xsl:comment> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:element name="xsl:template"> <xsl:attribute name="match" select="'/'" /> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" select="'node()'" /> </xsl:element> </xsl:element> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:element name="xsl:template"> <xsl:attribute name="match" select="'node()'" /> <xsl:element name="xsl:if"> <xsl:attribute name="test" select="'.!=&apos;&apos;'" /> <xsl:element name="xsl:copy"></xsl:element> </xsl:element> </xsl:element> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:comment> </xsl:comment> <xsl:comment> ............................................................................................... </xsl:comment> <xsl:apply-templates /> </xsl:element> <xsl:comment> ............................................................................................... </xsl:comment> </xsl:template> <xsl:template match="xs:complexType[@name]"> <xsl:element name="xsl:template"> <xsl:attribute name="match" select="@name" /> <xsl:apply-templates /> </xsl:element> <xsl:comment> ............................................................................................... </xsl:comment> </xsl:template> <xsl:template match="xs:complexType[not(@*)]"> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" select="@name" /> <xsl:apply-templates /> </xsl:element> </xsl:template> <!-- xsl:template match="xs:simpleType[@name]"> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" select="@name" /> <xsl:apply-templates /> </xsl:element> </xsl:template --> <xsl:template match="xs:sequence"> <xsl:element name="xsl:copy"> <xsl:apply-templates /> </xsl:element> </xsl:template> <xsl:template match="xs:element[@name]"> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" select="@name" /> </xsl:element> </xsl:template> <xsl:template match="xs:attribute"> <xsl:element name="xsl:apply-templates"> <xsl:attribute name="select" select="concat( '@', @name )" /> </xsl:element> </xsl:template> <!-- xsl:template match="xs:element[@name]"> <xsl:text>&#xA;&#xA;</xsl:text> <xsl:element name="xsl:template"> <xsl:attribute name="match" select="@name" /> <xsl:text>&#xA;</xsl:text> <xsl:comment> auto generated stub for element <xsl:value-of select="@name" /> </xsl:comment> <xsl:text>&#xA;</xsl:text> </xsl:element> <xsl:apply-templates /> </xsl:template --> <xsl:template match="text()" /> </xsl:stylesheet> 

Note the use of xsl:element to create XSLT tags and create select and match attributes, quoting when selecting and escaping. Comment blocks are intended for visual splitting of the document root (to make it more readable), but not for other purposes. In addition, this requires an XSLT 2.0 processor. xsltproc users do not need to apply.

As in the previous answers, you will have to change it to one degree or another for your use case. I did this in such a way as to quickly create an accurate skeleton from which I could create a useful XSLT document, as well as automate the tedious work.

Naturally, I just spent hours developing and testing what at this moment I could probably do faster manually using grep, but at least it was interesting. Hope this helps someone, and improvements are welcome.

+3
source

Unlike the statement in your question, you can use a single XSLT file to convert from general CSV to XML. See Kernow Converter

0
source

As Jim Harrison said, there is no simple mapping between XSD and XSLT. But both of them are XML formats, therefore it is impossible to correlate them with each other. Depending on the general structure of the formats, a generalized XSLT could also be written to map any XML directly to CSV, if the structure is simple enough.

You will need to provide some examples of XML data, XSD, and the expected result.

0
source

Source: https://habr.com/ru/post/1396092/


All Articles