XML 表格到 LaTeX

标签 xml xslt latex

假设我有一个 XML 表格

<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(我从其他地方偷走了这个例子)。我想要的结果是

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

在大多数情况下,两种表格格式之间存在公平的一对一对应关系。因此,这在大多数情况下工作得很好:

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

问题是 &(在输出中变成了 &)。在 LaTeX 中,您需要在第三行的开头,也就是跨单元格所在的位置。 XML 表中不是这种情况。我怎样才能解决这个问题?

最佳答案

enter image description here

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

关于XML 表格到 LaTeX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27021926/

相关文章:

java - JAXB 将 boolean 值编码为复杂类型

xml - 解码具有可变子类型的 XML

latex - latex AMS对齐/对齐多个 “=”,空间太大

java - 在 Spring 中添加 Jaxb2 消息转换器打破了 Jackson2 json 映射

xml - golang XML 不解码

xslt - 如何在xslt中“collapse”而不是“normalize”空白

xml - 如何添加具有新 ID 的第二个元素?

javascript - 将 javascript 变量传递给 xsl 条件

r - 除了标题之外,如何为 latex 输出的 knitr 图添加注释或文本?

r - R Markdown 中的德语元音变音和文章