将具有rowspan和colspan的XHTML表转换为LaTeX
我一直在寻找将HTML表转换为LaTeX的合适解决方案。我发现以下问题与我的要求类似:将具有rowspan和colspan的XHTML表转换为LaTeX,html,xslt,latex,xslt-2.0,Html,Xslt,Latex,Xslt 2.0,我一直在寻找将HTML表转换为LaTeX的合适解决方案。我发现以下问题与我的要求类似: 但这两种方法对我当前的需求都没有100%的帮助,因为它不涉及使用colspan和rowsspan转换内容 输入HTML如下所示: <html> <head> </head> <body> <table border="1"> <tr> <td>This</td> <
colspan
和rowsspan
转换内容
输入HTML如下所示:
<html>
<head>
</head>
<body>
<table border="1">
<tr>
<td>This</td>
<td>is</td>
<td>a</td>
<td>test</td>
</tr>
<tr>
<td colspan="2">This is</td>
<td>a</td>
<td>test</td>
</tr>
<tr>
<td>This</td>
<td>is</td>
<td colspan="2">a test</td>
</tr>
<tr>
<td rowspan="2">This</td>
<td colspan="2">is a</td>
<td rowspan="2">test</td>
</tr>
<tr>
<td>is</td>
<td>a</td>
</tr>
</table>
</body>
</html>
我采用中@DavidCarlisle给出的解决方案,并修改如下:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
<xsl:apply-templates/>
\end{document}
</xsl:template>
<xsl:template match="table">
<xsl:variable name="noc" select="max(tr/sum(td/(@colspan/number(.),1)[1]))"/>
<xsl:text>\begin{tabular}{*{</xsl:text>
<xsl:value-of select="$noc"/>
<xsl:text>}{|l}|} </xsl:text>
<xsl:apply-templates select="tr[1]">
<xsl:with-param name="rspans" select="for $i in 1 to xs:integer($noc) return 0"/>
</xsl:apply-templates>
<xsl:text>\end{tabular}</xsl:text>
</xsl:template>
<xsl:template match="tr">
<xsl:param name="rspans"/>
<xsl:text/>% [<xsl:value-of select="$rspans"/>]
<xsl:variable name="tr" select="."/>
<xsl:for-each select="$rspans">
<xsl:variable name="c" select="position()"/>
<xsl:variable name="td" select="$tr/td[count($rspans[position() <=$c][.=0])]"/>
<xsl:if test=".=0">
<xsl:if test="$td/@rowspan[. > 1]">
<xsl:text>\multirow{</xsl:text>
<xsl:value-of select="$td/@rowspan"/>
<xsl:text>}{*}{</xsl:text>
</xsl:if>
<xsl:if test="$td/@colspan[. > 1]">
<xsl:text>\multicolumn{</xsl:text>
<xsl:value-of select="$td/@colspan"/>
<xsl:text>}{|l|}{</xsl:text>
</xsl:if>
<xsl:apply-templates select="$td"/>
<xsl:if test="$td/@colspan[. > 1]">}</xsl:if>
<xsl:if test="$td/@rowspan[. > 1]">}</xsl:if>
</xsl:if>
<xsl:choose>
<xsl:when test=". >1 and position()=last()">&\\\hline </xsl:when>
<xsl:when test="position()=last()">\\\hline </xsl:when>
<xsl:otherwise>&</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="following-sibling::tr[1]">
<xsl:with-param name="rspans" select="for $c in 1 to count($rspans)
return
($rspans[$c] +
td[count($rspans[position() <=$c][.=0])]/(@rowspan,1)[1]
-1)"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
\documentclass{standalone}
\usepackage{multirow}
\开始{document}
\结束{document}
\开始{表格}{*{
}{l}}
;
\结束{表格}
% []
\多行{
}{*}{
\多列{
}{l}{
}
}
&\\\hline
;
\\\hline
;
&;
生成的LaTeX代码包含以下问题:
\cline{…}
命令<如果当前行中有跨行,则需要code>\cline{..}。否则需要\hline
\\\hline
前面的末尾出现了一个额外的&(与号)符号<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
<xsl:apply-templates/>
\end{document}
</xsl:template>
<xsl:template match="table">
<xsl:variable name="noc" select="max(tr/sum(td/(@colspan/number(.),1)[1]))"/>
<xsl:text>\begin{tabular}{*{</xsl:text>
<xsl:value-of select="$noc"/>
<xsl:text>}{|l}|} </xsl:text>
<xsl:apply-templates select="tr[1]">
<xsl:with-param name="rspans" select="for $i in 1 to xs:integer($noc) return 0"/>
</xsl:apply-templates>
<xsl:text>\end{tabular}</xsl:text>
</xsl:template>
<xsl:template match="tr">
<xsl:param name="rspans"/>
<xsl:text/>% [<xsl:value-of select="$rspans"/>]
<xsl:variable name="tr" select="."/>
<xsl:for-each select="$rspans">
<xsl:variable name="c" select="position()"/>
<xsl:variable name="td" select="$tr/td[count($rspans[position() <=$c][.=0])]"/>
<xsl:if test=".=0">
<xsl:if test="$td/@rowspan[. > 1]">
<xsl:text>\multirow{</xsl:text>
<xsl:value-of select="$td/@rowspan"/>
<xsl:text>}{*}{</xsl:text>
</xsl:if>
<xsl:if test="$td/@colspan[. > 1]">
<xsl:text>\multicolumn{</xsl:text>
<xsl:value-of select="$td/@colspan"/>
<xsl:text>}{|l|}{</xsl:text>
</xsl:if>
<xsl:apply-templates select="$td"/>
<xsl:if test="$td/@colspan[. > 1]">}</xsl:if>
<xsl:if test="$td/@rowspan[. > 1]">}</xsl:if>
</xsl:if>
<xsl:choose>
<xsl:when test=". >1 and position()=last()">&\\\hline </xsl:when>
<xsl:when test="position()=last()">\\\hline </xsl:when>
<xsl:otherwise>&</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="following-sibling::tr[1]">
<xsl:with-param name="rspans" select="for $c in 1 to count($rspans)
return
($rspans[$c] +
td[count($rspans[position() <=$c][.=0])]/(@rowspan,1)[1]
-1)"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>