[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML-to-XML question

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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.