2012-05-01 53 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