W3C home > Mailing lists > Public > www-svg@w3.org > December 2002

Re: SVG style

From: J. David Eisenberg <catcode@catcode.com>
Date: Sun, 8 Dec 2002 18:44:55 -0600 (CST)
To: Hyunju Shim <hshim@cise.ufl.edu>
cc: www-svg@w3.org
Message-ID: <Pine.LNX.4.44.0212081836210.28265-100000@tengu.host4u.net>

On Sun, 8 Dec 2002, Hyunju Shim wrote:

> I have  a SVG drawing tool Mayura, which generate following code.
> 
>       <rect style="stroke:#000000;stroke-width:0.5;fill:#c9c9ff" x="10" y="10" width="10" height="10"/>
> 
> However, I can not access the attribute via setAttribute to change any of stroke, stroke-width, and fill.
> So I need to change the node into following:
> 
>        <rect stroke="#000000" stroke-width="0.5" fill="#c9c9ff" x="10" y="10" width="10" height="10"/>
> 
> Can I make this transformatin with XSLT? 
> If so how?

Here's a transformation that expands the single "style" attribute into the
components that you want.  I won't claim that it's the best way to do it, 
or even an efficient method, but it works.  (Tested using Xalan-J 2.4.1
on a Linux box.)
==============================================================
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

<xsl:output method="xml" indent="yes" />

<!--
	This template adapted from the one on page 435 of
	the book XSLT, by Doug Tidwell, published by
	O'Reilly & Associates
-->
<xsl:template match="*">
	<xsl:element name="{name()}">
		<xsl:for-each select="@*">
			<xsl:choose>
			<xsl:when test="name()='style'">
				<xsl:call-template name="expand-style">
					<xsl:with-param name="stylestr" select="."/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:attribute name="{name()}"><xsl:value-of
					select="."/></xsl:attribute>
			</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
		<xsl:apply-templates select="*|text()"/>
	</xsl:element>
</xsl:template>

<!--
	Here's the template that splits the style into
	separate attributes and values.  It uses the
	normalize-space( ) function to get rid of any extra
	blanks around the : and ; in the style attribute.
	Written by JDEisenberg
-->

<xsl:template name="expand-style">
	<xsl:param name="stylestr"/>
	<xsl:if test="contains($stylestr,';') or 
		normalize-space($stylestr) !=''">
		<xsl:variable name="attrValPair" select="substring-before($stylestr,';')"/>
		<xsl:variable name="attr"
			select="normalize-space(substring-before($attrValPair,':'))"/>
		<xsl:variable name="value"
			select="normalize-space(substring-after($onespec,':'))"/>
		<xsl:attribute name="{$attr}"><xsl:value-of
			select="$value"/></xsl:attribute>
		<xsl:call-template name="expand-style">
			<xsl:with-param name="stylestr"
				select="substring-after($stylestr,';')"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
=======================================================
			

> It seems 'style' is one automic attribute so that there is no way to
> access a single element in it. Do I need to parse the document 
instead
> of XSL transformation? Can somebody help me?

-- 
J. David Eisenberg  http://catcode.com/
Received on Sunday, 8 December 2002 19:47:47 GMT

This archive was generated by hypermail 2.3.1 : Friday, 8 March 2013 15:54:24 GMT