[xsl] Turning css (font-weight:bold) attributes into tags <b>

Subject: [xsl] Turning css (font-weight:bold) attributes into tags <b>
From: "Rick Livesey" <rick.livesey@xxxxxxxxxxxx>
Date: Tue, 13 Mar 2007 10:39:59 -0400
Just wondered if someone could take a quick look at this XSL problem and
tell me if you have any better ideas than my hackery:

I need to convert XHTML backwards into an awful subset of HTML 3.2

One of the requirements is that I need to convert CSS-styles like this:

<span style="font-weight:bold;font-family:Times New
Roman;font-size:22pt;">ICAP plc</span>

into old-school HTML tags like this:

<span style="font-family:Times New Roman;font-size:22pt;"><b>ICAP
plc</b></span>

This would be easy enough if I only needed to extract the font-weight,
but I will also have to extract several other formatting codes from the
style attribute (vertical-align:sup, font-style:italics,etc), so need to
come up with a vaguely generic solution.

Unless I'm missing something, XSLT isn't really predisposed to parsing
an attribute's value multiple times in order to create new elements and
because of this the best solution I can come up with is:


 <xsl:template name="ExtractStyleAttributeToTags" >

  <!-- Remove each of the sub-attributes you need to turn into tags-->
  <xsl:variable name="styleAfterFontWeightRemoval"
select="XSLFunctions:RemoveAllOccOfStringFromSequence(tokenize(@style,';
'),'font-weight:bold',';')"/>
  <xsl:variable name="boldFound" select="@style !=
$styleAfterFontWeightRemoval" />
  <xsl:variable name="styleAfterFontAndVertAlignRemoval"
select="XSLFunctions:RemoveAllOccOfStringFromSequence(tokenize($styleAft
erFontWeightRemoval,';'),'vertical-align:sup',';')"/>
  <xsl:variable name="supFound" select="$styleAfterFontWeightRemoval !=
$styleAfterFontAndVertAlignRemoval" />

  <xsl:if test="$boldFound" >
   <xsl:text disable-output-escaping="yes">&lt;b&gt;</xsl:text>
  </xsl:if>
  <xsl:if test="$supFound" >
   <xsl:text disable-output-escaping="yes">&lt;sup&gt;</xsl:text>
  </xsl:if>

  <xsl:apply-templates select="node()"/>

  <xsl:if test="$supFound" >
   <xsl:text disable-output-escaping="yes">&lt;/sup&gt;</xsl:text>
  </xsl:if>
  <xsl:if test="$boldFound" >
   <xsl:text disable-output-escaping="yes">&lt;/b&gt;</xsl:text>
  </xsl:if>

 </xsl:template>

I realise that the 'disable-output-escaping' method is a hack, but since
my tags are conditional I'm not sure how to get around this but there
must be a better solution out there and as an XSLT beginner, I'm a bit
stumped.

Can anyone point me in the right direction?

Many thanks
Rick

Current Thread