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="'.!='''" /> <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: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="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.
source share