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" />
  </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


Current Thread