Parsing CSS style-like attributes with XSLT
03 January 2009
p.. Lets say we need to transform @<div style="width:236px; height:33px" />@ into @
The following snippet does it with the help of the function @getCssProperty@:
<xsl:template match="div" >
<xsl:variable name="height"
select="substring-before(melon:getCssProperty(@style,'height'),'px')"/>
<xsl:variable name="width"
select="substring-before(melon:getCssProperty(@style,'width'),'px')"/>
<element width="{$width}" height="{$height}" />
</xsl:template>
getCssProperty definition:
<xsl:function name="melon:getCssProperty">
<xsl:param name="style"/>
<xsl:param name="propertyName"/>
<xsl:for-each select="tokenize($style,';')">
<xsl:if test="normalize-space(substring-before(.,':'))=$propertyName">
<xsl:value-of select="normalize-space(substring-after(.,':'))" />
</xsl:if>
</xsl:for-each>
</xsl:function>
Update: I noticed XSLT 1.0 does not support the construct @
<xsl:variable name="height"
select="normalize-space(substring-before(substring-after(@style,'height:'),'px'))"/>
<xsl:variable name="width"
select="normalize-space(substring-before(substring-after(@style,'width:'),'px'))"/>
blog comments powered by Disqus