xsl-list
[Top] [All Lists]

RE: need help with converting attribute into elements

2003-10-14 03:03:14
Hi,

Yes, that would probably work, but I am required not to use CSS.

Sigh.

How about

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:x="x"
                exclude-result-prefixes="x">
  <xsl:template match="text">
    <font>
      <xsl:call-template name="decorator"/>
    </font>
  </xsl:template>
  <xsl:template name="decorator">
    <xsl:param name="node-set" select="@*"/>
    <xsl:choose>
      <xsl:when test="$node-set[1] = 'true'">
        <xsl:element name="{$map[(_at_)key = name($node-set[1])]/@value}">
          <xsl:call-template name="decorator">
            <xsl:with-param name="node-set" select="$node-set[not(position() = 
1)]"/>
          </xsl:call-template>
        </xsl:element>
      </xsl:when>
      <xsl:when test="$node-set">
        <xsl:call-template name="decorator">
          <xsl:with-param name="node-set" select="$node-set[not(position() = 
1)]"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:variable name="map" select="document('')/*/x:map/item"/>
  <x:map>
    <item key="bold" value="b"/>
    <item key="italic" value="i"/>
    <item key="underline" value="u"/>
  </x:map>
</xsl:stylesheet>

A tad verbose, granted, but fairly easy to customize to take care for blink and 
so forth. Or, if you wish to go the CSS-way when those-who-require-silly-things 
come to their senses,

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:x="x"
                exclude-result-prefixes="x">
  <xsl:template match="text">
    <span>
      <xsl:attribute name="style">
        <xsl:for-each select="@*[. = 'true']">
          <xsl:value-of select="$map[(_at_)key = name(current())]/@value"/>;
          <xsl:text/>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:apply-templates/>
    </span>
  </xsl:template>
  <xsl:variable name="map" select="document('')/*/x:map/item"/>
  <x:map>
    <item key="bold" value="font-weight:bold"/>
    <item key="italic" value="font-style:italic"/>
    <item key="underline" value="text-decoration:underline"/>
  </x:map>
</xsl:stylesheet>

Of course, use xsl:choose structure instead of a mapping table if it feels more 
natural to you. Or should that be "--for you--"?

Cheers,

Jarno - Conjure One: Sleep (Ian Van Dahl Mix) 

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list