Last active
December 16, 2015 23:49
-
-
Save aih/5516374 to your computer and use it in GitHub Desktop.
XSLT to transform xml to html generically. Converts all elements to divs with class= elementName, and all attributes to data-attributes. Updated to include text nodes and format output. Elements with an "inline" attribute are converted to spans.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:strip-space elements="*" /> | |
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> | |
<xsl:template match="node()|@*"> | |
<xsl:copy> | |
<xsl:apply-templates select="@*|node()"/> | |
</xsl:copy> | |
</xsl:template> | |
<xsl:template match="/"> | |
<html> | |
<body> | |
<h2>Transformed XML</h2> | |
<xsl:apply-templates select="/*"/> | |
</body> | |
</html> | |
</xsl:template> | |
<xsl:template match="node()"> | |
<div> | |
<xsl:attribute name="class"> | |
<xsl:value-of select="local-name(.)"/> | |
</xsl:attribute> | |
<xsl:attribute name="draggable">true</xsl:attribute> | |
<xsl:for-each select="@*"> | |
<xsl:attribute name="{concat('data-',local-name(.))}"> | |
<xsl:value-of select="."/> | |
</xsl:attribute> | |
</xsl:for-each> | |
<xsl:value-of select="text()"/> | |
<xsl:apply-templates select="*"/> | |
</div> | |
</xsl:template> | |
<xsl:template match="node()[@inline]"> | |
<span> | |
<xsl:attribute name="class"> | |
<xsl:value-of select="local-name(.)"/> | |
</xsl:attribute> | |
<xsl:for-each select="@*"> | |
<xsl:attribute name="{concat('data-',local-name(.))}"> | |
<xsl:value-of select="."/> | |
</xsl:attribute> | |
</xsl:for-each> | |
<xsl:value-of select="text()"/> | |
<xsl:apply-templates select="*"/> | |
</span> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment