Skip to content

Instantly share code, notes, and snippets.

@mrmeszaros
Created March 30, 2017 17:50
Show Gist options
  • Save mrmeszaros/500a151d5a712165d1ccff46b227c4c7 to your computer and use it in GitHub Desktop.
Save mrmeszaros/500a151d5a712165d1ccff46b227c4c7 to your computer and use it in GitHub Desktop.
XSLT 2.0 script to convert a few XML attributes into CSS style
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- can be used with `saxonb-xslt` -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" />
<!-- Select which attributes to transform -->
<xsl:variable name="selected" select="tokenize('stop-color stop-opacity opacity fill', '\s+')" />
<!-- Transformation -->
<xsl:template match="*">
<!-- Copy the element -->
<xsl:element name="{name()}">
<!-- Gather selected attributes -->
<xsl:variable name="style">
<xsl:for-each select="@*[index-of($selected, name(.)) != 0]">
<xsl:value-of select="concat(name(.), ':', .)"/>
<xsl:if test="not(last() = position())">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- Copy other attributes -->
<xsl:for-each select="@*[not(index-of($selected, name(.)))]">
<xsl:copy/>
</xsl:for-each>
<!-- Add style, if needed -->
<xsl:if test="string-length($style)">
<xsl:attribute name="style"><xsl:value-of select="$style"/></xsl:attribute>
</xsl:if>
<!-- Apply recursively -->
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment