使用 XSLT 将 XHTML 表转换为 LaTeX [英] Converting XHTML table to LaTeX using XSLT
问题描述
我是 XSLT (v1.0) 的新手,我无法使用 XSLT 将复杂的 XHTML 表转换为 LaTeX.
I'm new in XSLT (v1.0) and I can't convert complex XHTML tables to LaTeX using XSLT.
我所说的复杂表格是指具有不同列数的行的表格.换句话说,td
和 colspan
.
What I mean when I said complex tables, are tables with rows with different number of columns. In other words, td
with colspan
.
即(xhtml 表)
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="68" colspan="3"> <p>Values</p> </td>
</tr>
<tr>
<td valign="top" width="68"> <p>95</p> </td>
<td valign="top" width="68"> <p>169</p> <p> </p> </td>
<td valign="top" width="68"> <p>180</p> <p> </p> </td>
</tr>
</table>
我在 XSL 文件中所做的是:
What I'm doing in the XSL file is:
<xsl:template match="xhtml:table[@border='1']">
<xsl:text>\begin{center}</xsl:text>
<xsl:text>\begin{tabular}{</xsl:text>
<xsl:for-each select="xhtml:tr[1]/*">
<xsl:text>c</xsl:text>
<xsl:if test="position() = last()">
<xsl:text>} </xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>\toprule </xsl:text>
<xsl:for-each select="xhtml:tr">
<xsl:if test="position() != 1">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:if test="position() = 2">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:for-each select="xhtml:td|xhtml:th">
<xsl:if test="name() = 'th'">{\bf </xsl:if>
<xsl:apply-templates />
<xsl:if test="name() = 'th'">}</xsl:if>
<xsl:if test="position() != last()">
<xsl:text>&</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> \\ </xsl:text>
</xsl:for-each>
<xsl:text>\bottomrule </xsl:text>
<xsl:text>\end{tabular} </xsl:text>
<xsl:text>\end{center} </xsl:text>
</xsl:template>
但是正如你所看到的,这段代码只适用于简单的表格,没有 colspan 属性.代码围绕第一个 tr
循环,并为每个 td
写一个c".所以,在上面的例子中,它只会创建一个一列表.
But as you can see, this code just works for simple tables, without the colspan attribute. The code loops around the first tr
, and for each td
, it writes an "c". So, in the case above, it will only create a one column table.
我想要做的是计算 td
的数量,以及 colspan 的数量(如果存在),以创建一个包含 3 列的正确表.
What I want to do is count the number of td
, and the number of colspans if it exists, to create a correct table, with 3 columns.
有人知道怎么做吗?提前致谢.
Does anyone knows how to do this? Thanks in advance.
推荐答案
这在 XSLT2 中更容易,但您可以使用 (///*)[position() <= n]
XSLT 1 中的习惯用法迭代 n 次.我还稍微修正了你的 TeX:\bf
自 1993 年发布 latex2e 以来已被弃用:-)
This is easier in XSLT2 but you can use the (//*)[position() <= n]
idiom in XSLT 1 to iterate n times. I also fixed up your TeX a bit: \bf
has been deprecated since latex2e released in back in 1993:-)
<table xmlns="http://www.w3.org/1999/xhtml"
border="1" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="68" colspan="3"> <p>Values</p> </td>
</tr>
<tr>
<td valign="top" width="68"> <p>95</p> </td>
<td valign="top" width="68"> <p>169</p> <p> </p> </td>
<td valign="top" width="68"> <p>180</p> <p> </p> </td>
</tr>
</table>
<小时>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:output method="text"/>
<xsl:template match="xhtml:table[@border='1']">
<xsl:text>\begin{center} </xsl:text>
<xsl:text>\begin{tabular}{</xsl:text>
<xsl:for-each select="xhtml:tr[1]/*">
<xsl:choose>
<xsl:when test="@colspan">
<xsl:for-each select="(//*)[position()<=current()/@colspan]">c</xsl:for-each>
</xsl:when>
<xsl:otherwise>c</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>} </xsl:text>
<xsl:text>\toprule </xsl:text>
<xsl:for-each select="xhtml:tr">
<xsl:if test="position() != 1">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:if test="position() = 2">
<xsl:text>\midrule </xsl:text>
</xsl:if>
<xsl:for-each select="xhtml:td|xhtml:th">
<xsl:if test="self::xhtml:th">\bfseries </xsl:if>
<xsl:apply-templates />
<xsl:if test="position() != last()">
<xsl:text>&</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:if test="position()!=last()"> \\ </xsl:if>
</xsl:for-each>
<xsl:text>\end{tabular} </xsl:text>
<xsl:text>\end{center}</xsl:text>
</xsl:template>
</xsl:stylesheet>
<小时>
\begin{center}
\begin{tabular}{ccc}
\toprule
Values \\
\midrule
\midrule
95 & 169 & 180 \end{tabular}
\end{center}
这篇关于使用 XSLT 将 XHTML 表转换为 LaTeX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!