Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting XHTML table to LaTeX using XSLT

Tags:

xml

xslt

I'm new in XSLT (v1.0) and I can't convert complex XHTML tables to LaTeX using XSLT.

What I mean when I said complex tables, are tables with rows with different number of columns. In other words, td with colspan.

i.e. (xhtml table)

<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>

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>}&#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>

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.

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.

like image 725
Wagner Avatar asked Oct 31 '13 20:10

Wagner


People also ask

How to convert HTML table to Latex table?

Edit your HTML Table online, if needed You can edit your data online like Excel through Table Editor, and the changes will be converted into LaTeX Table in real-time. 3. Copy the converted LaTeX Table The options on the left side of the Table Generatorpanel can help you define LaTeX tables flexibly.

How to convert an XML file to XHTML?

Tip: To view the raw XML source, right-click in XML file and select "View Source"! View "cdcatalog.xml" Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template: Add the XSL style sheet reference to your XML document ("cdcatalog.xml"): . . If you have an XSLT compliant browser it will nicely transform your XML into XHTML.

How to convert an XML file to XSL style sheet?

Tip: To view the raw XML source, right-click in XML file and select "View Source"! View "cdcatalog.xml" Then you create an XSL Style Sheet ("cdcatalog.xsl") with a transformation template: Add the XSL style sheet reference to your XML document ("cdcatalog.xml"):

How do I convert an HTML table to a database?

Upload or paste your HTML Table Just paste (copy html source code from a browser) or drag-and-drop your HTML file into the textarea of Data Source, and it will immediately perform the magic of the conversion. The HTML table converter will automatically search for tables from the html source code you provide.


1 Answers

This is easier in XSLT2 but you can use the (//*)[position() &lt;= 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}&#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}
like image 86
David Carlisle Avatar answered Oct 07 '22 23:10

David Carlisle