我是XSLT(v1.0)的新手,我无法使用XSLT将复杂的XHTML表转换为LaTeX.
当我说复杂的表时,我的意思是具有不同列数的行的表.换句话说,td
用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>
Run Code Online (Sandbox Code Playgroud)
我在XSL文件中做的是:
<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>
Run Code Online (Sandbox Code Playgroud)
但正如您所看到的,此代码仅适用于没有colspan属性的简单表.代码循环第一个tr
,每个代码都td
写一个"c".因此,在上面的情况中,它只会创建一个列表.
我想要做的是计算td
colspans的数量和colspans的数量(如果存在),以创建一个包含3列的正确表.
有谁知道怎么做?提前致谢.
这在XSLT2中更容易,但您可以使用(//*)[position() <= n]
XSLT 1中的习惯来迭代n次.我还修改了你的TeX:\bf
自1993年在latex2e发布以来已被弃用:-)
<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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
\begin{center}
\begin{tabular}{ccc}
\toprule
Values \\
\midrule
\midrule
95 & 169 & 180 \end{tabular}
\end{center}
Run Code Online (Sandbox Code Playgroud)