xsl-list
[Top] [All Lists]

RE: [xsl] reduce xsl code...

2006-11-14 11:51:53
I don't think you can reduce this code all that much, but you can certainly
avoid repeating it. Simplest way is to put it in a named template, but I
would be tempted to use template rules, along the lines:

<xsl:template match="h3">
  ...
  <xsl:attribute name="style">
    <xsl:apply-templates select="@*"/>
  </xsl:attribute>
  ...
</xsl:template>


<xsl:template match="@bold[.='true']">font-weight:bold;</xsl:template>
<xsl:template match="@italic[.='true']">font-style:italic;</xsl:template>


etc

-----Original Message-----
From: Web Developer [mailto:armyofda12mnkeys(_at_)gmail(_dot_)com] 
Sent: 14 November 2006 18:44
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] reduce xsl code...

Hello all,

I created an xhtml-like schema and made 2 xsl files to 
translate the respected xml file into html or pdf.

Anyway, I was wondering if the code similar to code below can 
be reduced somehow by grouping it together in the 1 XSL file.

html xsl file...

<xsl:template match="h3">
<h3>

<!-- set default margin if not set -->
<xsl:variable name="marginVal">
<xsl:choose>
<xsl:when test="@margin">margin:<xsl:value-of 
select="@margin"/>;</xsl:when> <xsl:otherwise>
      <xsl:choose>            
              <xsl:when test="@margin-top |@margin-right 
|@margin-bottom| @margin-left">
                      <xsl:if 
test="@margin-top">margin-top:<xsl:value-of
select="@margin-top"/>;</xsl:if>
                      <xsl:if 
test="@margin-right">margin-right:<xsl:value-of
select="@margin-right"/>;</xsl:if>
                      <xsl:if 
test="@margin-bottom">margin-bottom:<xsl:value-of
select="@margin-bottom"/>;</xsl:if>
                      <xsl:if 
test="@margin-left">margin-left:<xsl:value-of
select="@margin-left"/>;</xsl:if>
              </xsl:when>
              <xsl:otherwise>margin:5px 0px 0px 0px;</xsl:otherwise>  
      </xsl:choose>
</xsl:otherwise>      
</xsl:choose>

</xsl:variable>


<xsl:if test="@color |@background-color |@text-indent|@text-align
|@font-family |@font-size |@border-style |@border-color 
|@border-width 
|@border| @border-top | @border-right | @border-bottom | @border-left 
|@padding | @padding-top | @padding-right | @padding-bottom |
@padding-left | $marginVal |@bold
|@italic|@underline|@vertical-align">
<xsl:attribute name="style">
      <xsl:if test="@bold='true'">font-weight:bold;</xsl:if>
      <xsl:if test="@italic='true'">font-style:italic;</xsl:if>
      <xsl:if 
test="@underline='true'">text-decoration:underline;</xsl:if>
      <xsl:if test="@color">color:<xsl:value-of 
select="@color"/>;</xsl:if>
      <xsl:if test="@background-color">background-color:<xsl:value-of
select="@background-color"/>;</xsl:if>
      <xsl:if test="@text-indent">text-indent:<xsl:value-of
select="@text-indent"/>;</xsl:if>
      <xsl:if test="@text-align">text-align:<xsl:value-of
select="@text-align"/>;</xsl:if>
      <xsl:if test="@font-family">font-family:<xsl:value-of
select="@font-family"/>;</xsl:if>
      <xsl:if test="@font-size">font-size:<xsl:value-of
select="@font-size"/>;</xsl:if>
      <xsl:if test="@border-style">border-style:<xsl:value-of
select="@border-style"/>;</xsl:if>
      <xsl:if test="@border-color">border-color:<xsl:value-of
select="@border-color"/>;</xsl:if>
      <xsl:if test="@border-width">border-width:<xsl:value-of
select="@border-width"/>;</xsl:if>
      <xsl:if test="@border">border:<xsl:value-of 
select="@border"/>;</xsl:if>
      <xsl:if test="@border-top">border-top:<xsl:value-of
select="@border-top"/>;</xsl:if>
      <xsl:if test="@border-right">border-right:<xsl:value-of
select="@border-right"/>;</xsl:if>
      <xsl:if test="@border-bottom">border-bottom:<xsl:value-of
select="@border-bottom"/>;</xsl:if>
      <xsl:if test="@border-left">border-left:<xsl:value-of
select="@border-left"/>;</xsl:if>
      <xsl:if test="@vertical-align">vertical-align:<xsl:value-of
select="@vertical-align"/>;</xsl:if>
      <xsl:if test="@padding">padding:<xsl:value-of 
select="@padding"/>;</xsl:if>         
      <xsl:if test="@padding-top">padding-top:<xsl:value-of
select="@padding-top"/></xsl:if>
      <xsl:if test="@padding-right">padding-right:<xsl:value-of
select="@padding-right"/></xsl:if>
      <xsl:if test="@padding-bottom">padding-bottom:<xsl:value-of
select="@padding-bottom"/></xsl:if>
      <xsl:if test="@padding-left">padding-left:<xsl:value-of
select="@padding-left"/></xsl:if>
              
      <xsl:if test="$marginVal"><xsl:value-of 
select="$marginVal"/></xsl:if>                
      
</xsl:attribute>
</xsl:if>
<xsl:value-of select="."/></h3>
</xsl:template>


Same template as above basically is repeated for match="h1" 
to "h6", but code starts getting a bit bulky.

plus the FO xsl file too can be shrinked using same principle 
someone here can recommend :).

Thanks in advance,
Arian

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>