2012-05-01 54 views
0

我在嘗試獲取每個td的寬度時出現問題,目前我的列寬中出現空白值,我如何獲取每個td的值。XSL - 計算表寬度

<xsl:template match="table"> 
    <fo:table table-layout="fixed"> 
    <xsl:call-template name="tokenize-style"/> 
     <!-- Calculate the table cols... --> 
     <xsl:for-each select="tbody/tr[1]/td"> 
     <fo:table-column> 
      <xsl:attribute name="column-width"> 
<xsl:value-of select="@width"></xsl:value-of> 

      </xsl:attribute> 
     </fo:table-column> 
     </xsl:for-each> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:table> 
    </xsl:template> 
    <xsl:template match="tbody"> 
    <fo:table-body> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:table-body> 
    </xsl:template> 

基於HTML - >

<table class="te-tablerenderer" style="width: 170mm; text-align: left;"> 
      <tbody style="text-align: left;"> 
       <tr class="" style="text-align: left;"> 
        <td style="width: 141px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); "> </td> 
        <td style="width: 143px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); ">   
        <b>Postcode:</b>   
        </td>  
        <td class="GENTICS_Table_Cell_active" 
         style="width: 325px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); ">   
        djnds fnjksdnf  
        </td>   
        <td style="width: 33px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: rgb(255, 255, 255); border-right-color: rgb(255, 255, 255); border-bottom-color: rgb(255, 255, 255); border-left-color: rgb(255, 255, 255); ">   </td>  
       </tr> 
      </tbody> 
     </table> 

注:這是我打電話的時候,我想我的一個元素的所有樣式的模板:

<xsl:template name="tokenize-style"> 
    <xsl:param name="pString" select="string(@style)"/> 
    <xsl:choose> 
     <xsl:when test="not($pString)"/> 
     <xsl:when test="contains($pString,';')"> 
     <xsl:call-template name="tokenize-style"> 
      <xsl:with-param name="pString" 
       select="substring-before($pString,';')"/> 
     </xsl:call-template> 
     <xsl:call-template name="tokenize-style"> 
      <xsl:with-param name="pString" 
       select="substring-after($pString,';')"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:attribute name="{normalize-space(substring-before($pString,':'))}"> 
      <xsl:value-of select="normalize-space(substring-after($pString,':'))"/> 
     </xsl:attribute> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 

輸出

<fo:table table-layout="fixed" width="170mm" text-align="left"> 
        <fo:table-column column-width=""/> 
        <fo:table-column column-width=""/> 
        <fo:table-column column-width=""/> 
        <fo:table-column column-width=""/> 
        <fo:table-body> 
        <fo:table-row> 
         <fo:table-cell padding-start="1pt" padding-end="1pt" padding-before="1pt" padding-after="1pt" 
             padding-top="1pt" 
             padding-bottom="1pt"> 
          <fo:block> </fo:block> 
         </fo:table-cell> 

(..等)

列寬爲空...

回答

2

的問題是,td都不具備的一個width屬性 - 你應該打電話給你tokenize-style模板上的每個td然後從它的輸出獲得的寬度,或者直接提取從style屬性的寬度 - 這樣的事情:

<xsl:attribute name="column-width"> 
    <xsl:value-of select="normalize-space(substring-before(substring-after(@syle,'width:'),';'))" /> 
    </xsl:attribute> 
+0

感謝,我現在要測試這一點... – Haroon