Re: [xsl] XML-to-XML question

Subject: Re: [xsl] XML-to-XML question
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Thu, 1 Mar 2001 10:45:45 +0000
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" />

So, you need to recursively go through the string and generate those
attribute definitions:

<xsl:template match="*[string(@style)]" mode="attributeise-style"
   <xsl:param name="style" select="@style" />
      <xsl:when test="contains($style, ',')">
         <xsl:call-template name="attributeise-property">
            <xsl:with-param name="property"
                            select="substring-before($style, ',')" />
         <xsl:call-template name="attributeise-style">
            <xsl:with-param name="style"
                            select="substring-after($style, ',')" />
      <xsl:when test="string($style)">
         <xsl:call-template name="attributeise-property">
            <xsl:with-param name="property" select="$string" />

<xsl:template name="attributeise-property">
   <xsl:param name="property" />
         name="{normalize-space(substring-before($property, ':'))}">
         select="normalize-space(substring-after($property, ':'))" />

You can call the above templates with:

<xsl:template match="svg">
      <xsl:apply-templates select="." mode="attributeise-style" />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread