I’m making an XSLT template in order to convert XML to LaTeX (then, HTML). I think XSLT wasn’t made for that, but it works. My only problem is text formatting : If in some lines of text, I want to use bold or italic words, the syntax is “< i >< / i >” in HTML for example, but “\textit{}” in LaTeX.
One solution is to declare “i” as a template, but I don’t know if I can apply it “automatically” for each encountred text block (I don’t want to call it explicitly in all my templates)
Sorry, I’m a newbie in this technology, maybe very simple solution exists, but Google didn’t help me this time.
Any suggestion will be appreciated.
EDIT : For example :
Xsl :
<xsl:template match="one">
<xsl:apply-templates select="two"/>
</xsl:template>
XML :
<one>
<two>Some text with <i>italic</i> words</two>
</one>
Desired output :
"Some text with \textit{italic} words"
And I don’t want to do :
<xsl:apply-templates select="i"/>
in all my templates
So I’m looking for a way to apply “globally” the “i” template.
As simple as this:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
The XSLT processing model uses a number of built-in templates, using which a default processing of any XML document is provided, should the XSLT programmer not provide a template matching a specific node.
In particular, the built-in template for any element node, just issues
<xsl:apply-templates/>so that templates are applied on all of its children.The built-in template for any text node is to copy this text node to the output.
This means that we don’t need to provide any template for a node, if it must do exactly what the corresponding built-in template does.
That leaves us only with an
ielement — so we provide a template matchingi— it simply surrounds by "\textit{" and "}" the result of processing its children.Do note:
It is entirely possible to express a complex transformation without ever specifying a single
<xsl:apply-templates>,<xsl:call-template>,<xsl:for-each>,<xsl:choose>,<xsl:when>,<xsl:otherwise>and<xsl:if>.This is called "push-style" as opposed by the "pull-style" where one or any of these instructions are extensively used.
The "push" style expresses the most declarative solution of a problem, while a "pull-style" transformation expresses a more "imperative" solution.
It is in the spirit of XSLT and recommended to always try to produce as much "push" code as possible.