Xml 使用XSLT将XHTML表转换为LaTeX

Xml 使用XSLT将XHTML表转换为LaTeX,xml,xslt,Xml,Xslt,我是XSLT(v1.0)新手,无法使用XSLT将复杂的XHTML表转换为LaTeX 我所说的复杂表是指具有不同列数的行的表。换句话说,td和colspan i、 e.(xhtml表) 数值 95 169 180 我在XSL文件中所做的是: <xsl:template match="xhtml:table[@border='1']"> <xsl:text>\begin{center}</xsl:text> <xsl:text>\b

我是XSLT(v1.0)新手,无法使用XSLT将复杂的XHTML表转换为LaTeX

我所说的复杂表是指具有不同列数的行的表。换句话说,
td
colspan

i、 e.(xhtml表)


数值

95

169 180
我在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>}&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>

    <xsl:text>\toprule&#10;</xsl:text>
    <xsl:for-each select="xhtml:tr">
        <xsl:if test="position() != 1">
            <xsl:text>\midrule&#10;</xsl:text>
        </xsl:if>

        <xsl:if test="position() = 2">
            <xsl:text>\midrule&#10;</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>&amp;</xsl:text>
            </xsl:if>
        </xsl:for-each>

        <xsl:text> \\&#10;</xsl:text>
    </xsl:for-each>

    <xsl:text>\bottomrule&#10;</xsl:text>

    <xsl:text>\end{tabular}&#10;</xsl:text>
    <xsl:text>\end{center}&#10;</xsl:text>
</xsl:template>

\开始{center}
\开始{表格}{
C
}

\最高规则
;
\中间规则
;
\中间规则
;
{\bf
}
&;
\\

\底部规则
;
\结束{表格}
;
\结束{中心}
;
但是正如您所看到的,这段代码只适用于简单的表,没有colspan属性。代码围绕第一个
tr
循环,对于每个
td
,它都写一个“c”。因此,在上述情况下,它将只创建一个单列表

我要做的是计算
td
的数量,以及colspan的数量(如果存在),以创建一个包含3列的正确表


有人知道怎么做吗?提前感谢。

这在XSLT2中更容易,但您可以使用XSLT1中的
(//*)[position()=n]
习惯用法迭代n次。我还对你的TeX做了一点修改:
\bf
自从1993年latex2e发布以来就被弃用了:-)



这应该在stackoverflow(它有一个非常活跃的XSLT标记)上询问,这不是TeX问题
<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>}&#10;</xsl:text>
        </xsl:if>
    </xsl:for-each>

    <xsl:text>\toprule&#10;</xsl:text>
    <xsl:for-each select="xhtml:tr">
        <xsl:if test="position() != 1">
            <xsl:text>\midrule&#10;</xsl:text>
        </xsl:if>

        <xsl:if test="position() = 2">
            <xsl:text>\midrule&#10;</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>&amp;</xsl:text>
            </xsl:if>
        </xsl:for-each>

        <xsl:text> \\&#10;</xsl:text>
    </xsl:for-each>

    <xsl:text>\bottomrule&#10;</xsl:text>

    <xsl:text>\end{tabular}&#10;</xsl:text>
    <xsl:text>\end{center}&#10;</xsl:text>
</xsl:template>
<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}&#10;</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()&lt;=current()/@colspan]">c</xsl:for-each>
   </xsl:when>
   <xsl:otherwise>c</xsl:otherwise>
  </xsl:choose>
 </xsl:for-each>
 <xsl:text>}&#10;</xsl:text>

 <xsl:text>\toprule&#10;</xsl:text>
 <xsl:for-each select="xhtml:tr">
  <xsl:if test="position() != 1">
   <xsl:text>\midrule&#10;</xsl:text>
  </xsl:if>

  <xsl:if test="position() = 2">
   <xsl:text>\midrule&#10;</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>&amp;</xsl:text>
   </xsl:if>
  </xsl:for-each>

  <xsl:if test="position()!=last()"> \\&#10;</xsl:if>
 </xsl:for-each>

 <xsl:text>\end{tabular}&#10;</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}