XML technologies / XSL-FO / Examples of application / Text blocks

 

Text blocks

In this example text blocks whose characteristics are controlled by the instance are generated.

Here, a template for the generation of texts blocks is demonstrated. These blocks can be formatted by the instance with the help of attributes.
So, in this example, characteristics as font size, alignment, indents, bold, italic etc. are determined.

Excerpt from the DTD:

<!ELEMENT TB (#PCDATA | ...)*>
<!ATTLIST TB
     Size (normal | large | small) "normal"
     Font (normal | bold | italic) "normal"
     Alignment (left | right | center | justify) "justify"
     IndentLeft (without | with | line1 | hanging) "without">

Stylesheet:

<xsl:template match="TB"> 
   <fo:block text-align="justify" hyphenate="true" xml:lang="en"           ❶
   margin-left="10mm">         
      <xsl:choose>
         <xsl:when test="@Size='large'">                                   ❷
            <xsl:attribute name="font-size">14pt</xsl:attribute>           ❸
         </xsl:when>
         <xsl:when test="@Size='small'"> 
            <xsl:attribute name="font-size">10pt</xsl:attribute>
         </xsl:when> 
         <xsl:otherwise>                                                   ❹   
            <xsl:attribute name="font-size">12pt</xsl:attribute>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:choose>
         <xsl:when test="@Font='bold'">                                    ❺
            <xsl:attribute name="font-weight">bold</xsl:attribute>
         </xsl:when>
         <xsl:when test="@Font='italic'">
            <xsl:attribute name="font-style">italic</xsl:attribute>
         </xsl:when> 
         <xsl:otherwise/>                                                  ❻
      </xsl:choose>
      <xsl:choose>
         <xsl:when test="@Alignment='left'">
            <xsl:attribute name="text-align">left</xsl:attribute>
         </xsl:when>
         <xsl:when test="@Alignment='right'"> 
            <xsl:attribute name="text-align">right</xsl:attribute>
         </xsl:when>
         <xsl:when test="@Alignment='center'">
            <xsl:attribute name="text-align">center</xsl:attribute>
         </xsl:when>
         <xsl:otherwise>
            <xsl:attribute name="text-align">justify</xsl:attribute>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:choose> 
         <xsl:when test="@IndentLeft='with'">
            <xsl:attribute name="margin-left">15mm</xsl:attribute>
         </xsl:when> 
         <xsl:when test="@IndentLeft='line1'">
            <xsl:attribute name="text-indent">10mm</xsl:attribute>
         </xsl:when> 
         <xsl:when test="@IndentLeft='hanging'">
            <xsl:attribute name="text-indent">-10mm</xsl:attribute> 
         </xsl:when>
         <xsl:otherwise/>
      </xsl:choose>
      <xsl:apply-templates/>
   </fo:block>
</xsl:template>

 

❶ Universally valid attributes are specified for the block: justification, English hyphenation, left indent.

❷ The further specification depends on the attributes and their values. The whole is embedded in the <xsl:choose> XSLT concept.

❸ Under the condition that the font size is large, the font size 14pt shall be set.

❹ Under the condition that the font size is normal or if the attribute is not set in the instance (because it is the default value), the font size 12pt is valid.

❺ The same as mentioned for the font size applies accordingly.

❻ The same as mentioned for the default value of the font size applies accordingly. Here, no attribute is specified. Therefore, <xsl:otherwise> stays empty.

back to top