This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: XML-to-XML question
- To: Wolfgang Bogacz <wbogacz at omicron dot com>
- Subject: Re: [xsl] XML-to-XML question
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Thu, 1 Mar 2001 10:45:45 +0000
- CC: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Organization: Jeni Tennison Consulting Ltd
- References: <BED89E201B46D311BAC100902765086A0ACE64@postmaster.omicron-usa.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Wolfgang,
> However, my incoming files contain style attributes in the form of a
> comma-delimited string, which I was trying to parse into individual
> attributes much like:
>
> <svg style="font-family:value1,font-size:value2,font-style:value3">
> which I want to output as:
> <svg font-family="value1" font-size="value2" font-style="value3">.
You can create attributes while setting their names and values
dynamically using the xsl:attribute element:
<xsl:attribute name="{$attr-name}">
<xsl:value-of select="$attr-value" />
</xsl:attribute>
So, you need to recursively go through the string and generate those
attribute definitions:
<xsl:template match="*[string(@style)]" mode="attributeise-style"
name="attributeise-style">
<xsl:param name="style" select="@style" />
<xsl:choose>
<xsl:when test="contains($style, ',')">
<xsl:call-template name="attributeise-property">
<xsl:with-param name="property"
select="substring-before($style, ',')" />
</xsl:call-template>
<xsl:call-template name="attributeise-style">
<xsl:with-param name="style"
select="substring-after($style, ',')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="string($style)">
<xsl:call-template name="attributeise-property">
<xsl:with-param name="property" select="$string" />
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="attributeise-property">
<xsl:param name="property" />
<xsl:attribute
name="{normalize-space(substring-before($property, ':'))}">
<xsl:value-of
select="normalize-space(substring-after($property, ':'))" />
</xsl:attribute>
</xsl:template>
You can call the above templates with:
<xsl:template match="svg">
<svg>
<xsl:apply-templates select="." mode="attributeise-style" />
</svg>
</xsl:template>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list