2010-08-17 62 views
4

我正在處理XSL模板以將XHTML/hResume文檔轉換爲純文本,並且我在表格佈局(不,不是佈局表格)中遇到了問題。目前,我已經得到了下面,使用優秀戴夫Pawson的padding templateXSLT輸出純文本表格

<variable name="newline" select="'&#10;'"/> 
<template match="xhtml:table"> 
    <variable name="maxWidth"> 
     <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td"> 
      <sort select="string-length(child::text()|child::node())" order="descending" data-type="number"/> 
      <if test="position() = 1"> 
       <value-of select="string-length(child::text()|child::node())"/> 
      </if> 
     </for-each> 
    </variable> 
    <for-each select="xhtml:tr"> 
     <for-each select="xhtml:th|xhtml:td"> 
      <variable name="string"> 
       <for-each select="child::text()|child::node()"> 
        <value-of select="."/> 
       </for-each> 
      </variable> 
      <value-of select="$string"/> 
      <call-template name="append-pad"> 
       <with-param name="length" select="$maxWidth - string-length($string)"/> 
      </call-template> 
      <text>&#32;</text> 
     </for-each> 
     <value-of select="$newline"/> 
    </for-each> 
    <value-of select="$newline"/> 
</template> 

這會產生寬度相等的列,但我想改善它在幾個方面:

  • 查找並使用每列的最大寬度。爲此,需要存儲靈活數量的值。在簡單的情況下,我可以改變maxWidth來做到這一點,但你如何處理跨越列?
  • 居中列的內容。

是否有任何模板可以做這樣的事情?

回答

4

着有 「全球」(在表中的每個單元格)$maxWith你可以處理colspans喜歡這個樣式表(保留自己的邏輯):

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <output method="text"/> 
    <variable name="newline" select="'&#10;'"/> 
    <template match="xhtml:table"> 
     <variable name="maxWidth"> 
      <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td"> 
       <sort select="string-length(child::text()|child::node())" order="descending" data-type="number"/> 
       <if test="position() = 1"> 
        <value-of select="string-length(child::text()|child::node())"/> 
       </if> 
      </for-each> 
     </variable> 
     <for-each select="xhtml:tr"> 
      <for-each select="xhtml:th|xhtml:td"> 
       <variable name="string"> 
        <for-each select="child::text()|child::node()"> 
         <value-of select="."/> 
        </for-each> 
       </variable> 
       <variable name="padding"> 
        <choose> 
         <when test="@colspan"> 
          <value-of select="$maxWidth * @colspan + @colspan - 1 - string-length($string)"/> 
         </when> 
         <otherwise> 
          <value-of select="$maxWidth - string-length($string)"/> 
         </otherwise> 
        </choose> 
       </variable> 
       <value-of select="$string"/> 
       <call-template name="append-pad"> 
        <with-param name="length" select="$padding"/> 
       </call-template> 
       <text>&#32;</text> 
      </for-each> 
      <value-of select="$newline"/> 
     </for-each> 
     <value-of select="$newline"/> 
    </template> 
    <template name="append-pad"> 
     <param name="length" select="0"/> 
     <if test="$length != 0"> 
      <value-of select="'&#32;'"/> 
      <call-template name="append-pad"> 
       <with-param name="length" select="$length - 1"/> 
      </call-template> 
     </if> 
    </template> 
</stylesheet> 

輸入:

<table xmlns="http://www.w3.org/1999/xhtml"> 
    <tr> 
     <th>First</th> 
     <th>Second</th> 
     <th>Third</th> 
    </tr> 
    <tr> 
     <td>One</td> 
     <td>Two</td> 
     <td>Three</td> 
    </tr> 
    <tr> 
     <td colspan="2">Uno</td> 
     <td>Tres</td> 
    </tr> 
</table> 

輸出:

First Second Third 
One Two Three 
Uno   Tres 

編輯:In或DER居中細胞合併單元格,使用這個樣式表(現在我自己的邏輯):

<stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml"> 
    <output method="text"/> 
    <variable name="newline" select="'&#10;'"/> 
    <template match="xhtml:table"> 
     <apply-templates> 
      <with-param name="maxWidth"> 
       <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td"> 
        <sort select="string-length(.)" order="descending" data-type="number"/> 
        <if test="position() = 1"> 
         <value-of select="string-length(.)"/> 
        </if> 
       </for-each> 
      </with-param> 
     </apply-templates> 
     <value-of select="$newline"/> 
    </template> 
    <template match="xhtml:tr"> 
     <param name="maxWidth"/> 
     <apply-templates> 
      <with-param name="maxWidth" select="$maxWidth"/> 
     </apply-templates> 
     <value-of select="$newline"/> 
    </template> 
    <template match="xhtml:th|xhtml:td"> 
     <param name="maxWidth"/> 
     <variable name="string"> 
      <for-each select="child::text()|child::node()"> 
       <value-of select="."/> 
      </for-each> 
     </variable> 
     <variable name="padding"> 
      <choose> 
       <when test="@colspan"> 
        <value-of select="($maxWidth * @colspan + @colspan - 1 - string-length($string)) div 2"/> 
       </when> 
       <otherwise> 
        <value-of select="$maxWidth - string-length($string)"/> 
       </otherwise> 
      </choose> 
     </variable> 
     <if test="@colspan"> 
      <call-template name="append-pad"> 
       <with-param name="length" select="floor($padding)"/> 
      </call-template> 
     </if> 
     <value-of select="$string"/> 
     <call-template name="append-pad"> 
      <with-param name="length" select="ceiling($padding)"/> 
     </call-template> 
     <text>&#32;</text> 
    </template> 
    <template name="append-pad"> 
     <param name="length" select="0"/> 
     <if test="$length != 0"> 
      <value-of select="'&#32;'"/> 
      <call-template name="append-pad"> 
       <with-param name="length" select="$length - 1"/> 
      </call-template> 
     </if> 
    </template> 
</stylesheet> 

輸出:

First Second Third 
One Two Three 
    Uno  Tres