假设我有一个如下形式的XML表格:
问题在于
<table>
<tr>
<td>First Name:</td>
<td>Bill Gates</td>
</tr>
<tr>
<td rowspan="2">Telephone:</td>
<td>555 77 854</td>
</tr>
<tr>
<td>555 77 855</td>
</tr>
</table>
我希望用XSLT将其转换为LaTeX(我从其他地方偷了这个例子)。 我想要的结果是
\documentclass{memoir}
\usepackage{multirow}
\begin{document}
\begin{tabular}{*{10}{c}}
First name & Bill Gates &\\
\multirow{2}{*}{Telephone:}
& 555 77 854 &\\
& 555 77 855 &
\end{tabular}
\end{document}
在大多数情况下,这两种表格格式之间存在着相当的一对一对应关系。因此,在大多数情况下,这样做效果非常好:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8"/>
<xsl:template match="/">
\documentclass{memoir}
\usepackage{multirow}
\begin{document}
<xsl:apply-templates/>
\end{document}
</xsl:template>
<xsl:template match="table">
\begin{tabular}{*{10}{c}}
<xsl:apply-templates/>
\end{tabular}
</xsl:template>
<xsl:template match="tr">
<xsl:apply-templates />\\
</xsl:template>
<xsl:template match="td[not(@rowspan) and not(@colspan)]">
<xsl:apply-templates/> &
</xsl:template>
<xsl:template match="td[not(@colspoan)]">
\multirow{<xsl:value-of select="@colspan"/>}{*}{<xsl:apply-templates/>} &
</xsl:template>
<xsl:template match="td[not(@rowspan)]">
\multicolumn{<xsl:value-of select="@colspan"/>}{l}{<xsl:apply-templates/>} &
</xsl:template>
<xsl:template match="td">
\multirow{<xsl:value-of select="@rowspan"/>}{*}{\multicolumn{<xsl:value-of select="@colspan"/>}}{l}{<xsl:apply-templates/>} &
</xsl:template>
</xsl:stylesheet>
问题在于
&
(输出时变为&
)。在LaTeX中,您需要在跨越单元格的第三行开头使用它。但在XML表格中不是这种情况。如何解决这个问题?
\multicolumn
,但在你的预期输出中实际上并没有显示出来。此外,XSLT引用了一个no
函数,我认为应该实际上是not
。还有一些语法错误。在匹配table
的模板中缺少一个"
,在匹配td[no(@rowspan)
的模板中缺少一个]
。如果你能纠正所有这些,那将会非常有帮助。谢谢! - Tim C\multicolumn
。但是大多数情况下都是\multirow
引起了问题。我为这个问题专门制作了一个简化版本。我会尝试纠正你提到的错误。 - Gaussler\cr
?(这不是 LaTeX 命令)我想你本意是&
,因为你在每个 td 中都添加了它们。 - David Carlisle