2013-10-31 12 views
6

Sono nuovo in XSLT (v1.0) e non riesco a convertire le tabelle XHTML complesse in LaTeX utilizzando XSLT.Conversione della tabella XHTML in LaTeX utilizzando XSLT

Cosa intendo quando ho detto tabelle complesse, sono tabelle con righe con diverso numero di colonne. In altre parole, td con colspan.

cioè (tabella 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> 

Quello che sto facendo nel file 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> 

Ma come si può vedere, questo codice funziona solo per le tabelle semplici, senza la attributo colspan. Il codice scorre attorno al primo tr e, per ogni td, scrive una "c". Quindi, nel caso precedente, creerà solo una tabella di colonne.

Quello che voglio fare è contare il numero di td, e il numero di colspane se esiste, per creare una tabella corretta, con 3 colonne.

Qualcuno sa come farlo? Grazie in anticipo.

+1

Questo dovrebbe essere chiesto su StackOverflow (che ha un tag XSLT molto attivo) Non è una domanda TeX –

risposta

6

Questo è più semplice in XSLT2 ma è possibile utilizzare l'idioma (//*)[position() &lt;= n] in XSLT 1 per iterare n volte. Ho anche sistemato il vostro TeX un po ': \bf è stata deprecata dal LaTeX2e uscito nel nel 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} 
+0

grazie mille! Stavo provando a ripararlo per alcuni giorni. – Wagner

Problemi correlati