Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT Properly applying formatting attributes

Status
Not open for further replies.

osirus100

Technical User
Jul 31, 2002
3
US
I'm new to XML so any help on this topic would be greatly appreciated.

I have this XML that needs to be translated to HTML. Therefore I have decided to use XSLT to do this. The XML has a lot of formatting attributes that I need to convert into HTML tags.

For example:

<Paragraph>
<Run FontStyle="Italic" FontWeight="Bold">Bold and Italic</Run>
<Run FontWeight="Bold">Bold</Run>
</Paragraph>

There are others formatting attributes and each Run tag may have many or no formatting attributes. Therefore all Italics need to be converted to <i>, bolds to <b>, etc.

I started to use nested <xsl:choose> statements to do this but then realized it would get nasty very quickly.

<xsl:choose>
<xsl:when test="./@FontWeight = 'Bold'">
<b>
<xsl:choose>
<xsl:when test="./@FontStyle='Italic'"> <i><xsl:value-of select="."/></i>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="."/>
</xsl:eek:therwise>
</xsl:choose>
</b>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="."/>
</xsl:eek:therwise>
</xsl:choose>

There must be a better way...any ideas?
 
How about this:
Code:
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] version="1.0">
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="Paragraph">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xsl:template match="Run">
    <span>
      <xsl:attribute name="style">
        <xsl:if test="@FontStyle">
          <xsl:text>font-style:</xsl:text>
          <xsl:value-of select="@FontStyle"/>
          <xsl:text>;</xsl:text>
        </xsl:if>
        <xsl:if test="@FontWeight">
          <xsl:text>font-weight:</xsl:text>
          <xsl:value-of select="@FontWeight"/>
          <xsl:text>;</xsl:text>
        </xsl:if>
      </xsl:attribute>
      <xsl:apply-templates/>
    </span>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top