2010-08-17 3 views
4

XSL 템플릿을 사용하여 XHTML/hResume 문서를 일반 텍스트로 변환하려고하는데 테이블 레이아웃 (레이아웃 테이블 아님)에 문제가 있습니다. 순간 나는 사용하여 다음있어 우수한 데이브 Pawson의 padding template :XSLT로 일반 텍스트 테이블 출력

<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를 변경할 수 있지만 스패닝 열은 어떻게 처리합니까?
  • 스패닝 열의 내용을 가운데에 배치하십시오.

이와 비슷한 템플릿이 있습니까? 는 "글로벌"이 스타일 시트 같은 colspans을 처리 할 수 ​​$maxWith (테이블의 모든 셀에 대해) (보존 자신의 논리)와

답변

4

:

<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 

편집 : 또는 데르 (내 자신의 논리와 지금)이 스타일 시트를 사용, 열 병합하여 세포를 중앙에 :

<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