@main[] ^header[Typografy through XSLT] ^maindir[2]
^portrait[ash]
Andrey Shitov ^email[ash;]

Typography through XSLT 1 April 2007


 
Task: Bring on a new approach to online typography.

All new sites designed in the studio that contain news, articles or any other publications use automatically typographically edited text. There exist at least 4 types of typograph software written in parser, javascript and C++. Following the trend, we decided to complete this set with a new typograph performing XSLT transformations.


Preparing text
First, initial text is transformed into XML format&1;it can be done easily with the help of a simple additional utility. The data furnished to XSLT processor looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<text>
    <paragraph>
        <quote type="double"/>
        <word caps="first">нОПЗЙЕ</word>
        <space/>
        <word>Many</word>
        <space/>
        <word>people</word>
        <space/>
        <word>get</word>
        <space/>
        <word type="article">a</word>
        <space/>
        <word>kick</word>
        <space/>
        <word type="preposition">out</word>
        <space/>
        <word type="preposition">of</word>
        <space/>
        <word>direct</word>
        <space/>
        <word>contact</word>
        <space/>
        <word type="preposition">with</word>
        <space/>
        <word>AC</word>
	<space/>
	<word>power</word>
	<space/>
	<word>supply</word>
	<quote type="double"/>
        <punctuation type="fullstop"/>
    </paragraph>
    <paragraph>
        <word caps="first" type="preposition">To</word>
        <space/>
        <word>that</word>
        <space/>
        <word>end</word>
        <punctuation type="comma"/>
        <space/>
        <word>they</word>
        <space/>
        <word>normally</word>
        <space/>
        <word>use</word>
        <word>
            <part caps="first">U</part>
            <part>shaped</part>
        </word>
        <space/>
        <word>fragments</word>
        <space/>
        <word type="preposition">of</word>
	<space/>
	<word>bare</word>
        <space/>
        <word>wire</word>
        <punctuation type="fullstop"/>
    </paragraph>
</text>
				

XSLT typograph script
XSLT excellently serves the purpose of embodying typography rules for any language. Moreover, a programmer has a choice of two techniques: defining rules in terms of XPath expressions directly in template match attribute, or using expressions xsl:choose and xsl:if.

Here is a fragment of XSLT file containing Russian typography rules:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output
    encoding="UTF-8"
    method="html"
/>

<xsl:template match="/text">
    <xsl:apply-templates select="paragraph"/>
</xsl:template>

<xsl:template match="paragraph">
    <p>
        <xsl:apply-templates select="word | space | punctuation | quote"/>
    </p>
</xsl:template>

<xsl:template match="word[not (part)]">
    <xsl:value-of select="text()"/>
</xsl:template>

<xsl:template match="word[part]">
    <nobr>
        <xsl:for-each select="part">
            <xsl:value-of select="text()"/>
            <xsl:if test="position() != last()">
                <xsl:text>-</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </nobr>
</xsl:template>

<xsl:template match="space">
    <xsl:choose>
        <xsl:when test="preceding-sibling::word[1][string-length (text()) &lt; 3]">
            <xsl:text disable-output-escaping="yes">&amp;0;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text> </xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="quote[following-sibling::*[1][name() = 'word']]">
    <xsl:text disable-output-escaping="yes">&amp;1;</xsl:text>
</xsl:template>

<xsl:template match="quote[preceding-sibling::*[1][name() = 'word']]">
    <xsl:text disable-output-escaping="yes">&amp;7;</xsl:text>
</xsl:template>

<xsl:template match="quote">
    <xsl:choose>
        <xsl:when test="@type = 'double'">
            <xsl:text>"</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>'</xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template match="punctuation[@type = 'fullstop']">
    <xsl:text>.</xsl:text>
</xsl:template>

<xsl:template match="punctuation[@type = 'comma']">
    <xsl:text>,</xsl:text>
</xsl:template>

</xsl:stylesheet>
				

If you wish to see how this script works, you are welcome to use the above examples and complete the rules on your own, if necessary.

I would recommend using automatic XSLT-based typography on high loaded servers.

^include[/bottomblok.html]