到LaTeX的XML表 [英] XML table to LaTeX

查看:84
本文介绍了到LaTeX的XML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个XML表格,形式为

Suppose I have an XML table of the form

<table>
  <tr>
    <td>First Name:</td>
    <td>Bill Gates</td>
  </tr>
  <tr>
    <td rowspan="2">Telephone:</td>
    <td>555 77 854</td>
  </tr>
  <tr>
    <td>555 77 855</td>
  </tr>
</table>

我希望使用XSLT转换为LaTeX的

(我在其他地方偷了这个示例).我想要的结果是

that I wish to convert to LaTeX using XSLT (I stole this example elsewhere). The result I want is

\documentclass{memoir}
\usepackage{multirow}
\begin{document}
\begin{tabular}{*{10}{c}}
 First name & Bill Gates &\\
 \multirow{2}{*}{Telephone:}
   & 555 77 854 &\\
   & 555 77 855 &
\end{tabular}
\end{document}

在大多数情况下,两种表格式之间存在一对一的对应关系.因此,这在大多数情况下效果很好:

For the most part, there is a fair one-to-one-correspondence between the two table formats. Thus this works quite well for the most part:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" encoding="UTF-8"/>

<xsl:template match="/">

\documentclass{memoir}
\usepackage{multirow}
\begin{document}

<xsl:apply-templates/>

\end{document}

</xsl:template>

<xsl:template match="table">
\begin{tabular}{*{10}{c}}
<xsl:apply-templates/>
\end{tabular}
</xsl:template>

<xsl:template match="tr">
<xsl:apply-templates />\\
</xsl:template>

<xsl:template match="td[not(@rowspan) and not(@colspan)]">
<xsl:apply-templates/> &amp;
</xsl:template>

<xsl:template match="td[not(@colspoan)]">
\multirow{<xsl:value-of select="@colspan"/>}{*}{<xsl:apply-templates/>} &amp;
</xsl:template>

<xsl:template match="td[not(@rowspan)]">
\multicolumn{<xsl:value-of select="@colspan"/>}{l}{<xsl:apply-templates/>} &amp;
</xsl:template>

<xsl:template match="td">
\multirow{<xsl:value-of select="@rowspan"/>}{*}{\multicolumn{<xsl:value-of select="@colspan"/>}}{l}{<xsl:apply-templates/>} &amp;
</xsl:template>
</xsl:stylesheet>

问题是&amp; s(在输出中变为&).在LaTeX中,您需要在跨行单元格所在的第三行的开头添加一个. XML表中不是这种情况.我该如何解决这个问题?

The problem is the &amp;s (which become & in the output). In LaTeX, you need one in the beginning of the third row, where the spanned cell is. This is not the case in the XML table. How can I get around this problem?

推荐答案

<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{memoir}
\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}}&#10;</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() &lt;=$c][.=0])]"/>
  <xsl:if test=".=0">
   <xsl:if test="$td/@rowspan[. &gt; 1]">
    <xsl:text>\multirow{</xsl:text>
    <xsl:value-of select="$td/@rowspan"/>
    <xsl:text>}{*}{</xsl:text>
   </xsl:if>
   <xsl:apply-templates select="$td"/>
   <xsl:if test="$td/@rowspan[. &gt; 1]">}</xsl:if>
  </xsl:if>
  <xsl:choose>
   <xsl:when test=". &gt;1 and position()=last()">&amp;\\&#10;</xsl:when>
   <xsl:when test="position()=last()">\\&#10;</xsl:when>
   <xsl:otherwise>&amp;</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() &lt;=$c][.=0])]/(@rowspan,1)[1]
   -1)"/>
 </xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>

这篇关于到LaTeX的XML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆