TeXML
Previous step: “PDF literal strings

TeXML and XSLT

TeXML code is too verbose and sometimes redundant to be authored manually. For example, on the previous step, the TeXML code contains the same string 3 times.

Actually, it's not a problem. TeXML is not intended to be created manually. Instead, TeXML is to be generated, preferrably by XSLT from XML.

Let's generate the TeXML code similar to that on the previous step. The source XML:

<document>
  <section>
    <title>Заголовок (Title)</title>
    <para>first paragraph</para>
    <para>second paragraph</para>
    ... more paragraphs ...
  </section>
  ... more sections ...
</document>

An XSLT program to convert such XML documents:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- convert "document": create header and continue -->
<xsl:template match="document">
  <TeXML>
    <!-- create header -->
    <TeXML escape="0">
\documentclass{article}
\usepackage[T2A]{fontenc}
\usepackage[koi8-r]{inputenc}
\usepackage[unicode]{hyperref}
    </TeXML>
    <!-- process content -->
    <env name="document">
      <xsl:apply-templates/>
    </env>
  </TeXML>
</xsl:template>

<!-- convert "para": process content and add "\par" -->
<xsl:template match="para">
  <xsl:apply-templates />
  <cmd name="par" gr="0" nl2="1" />
</xsl:template>

<!-- convert sections by converting "title" -->
<xsl:template match="title">
  <cmd name="section">
    <opt>
      <cmd name="texorpdfstring">
        <parm><xsl:value-of select="."/></parm>
        <parm><pdf><xsl:value-of select="."/></pdf></parm>
      </cmd>
    </opt>
    <parm><xsl:value-of select="."/></parm>
  </cmd>
</xsl:template>

</xsl:stylesheet>

On the one hand, the only achievement is that complexity is moved to XSLT. But on the other hand, it's a great improvement: the non-trivial construction is written only once (section command with the texorpdfstring and pdf commands inside the option). In addition, the complexity is hidden from the authors who write texts in XML and don't know much about LaTeX.

Next step: “Encodings


TeXML
This page: http://getfo.org/texml/tour_xslt.html