XSL - 计算表格宽度

时间:2012-05-01 14:20:57

标签: xslt xpath

我在尝试获取每个td的宽度时遇到问题,我目前在列宽中找到一个空白值,我如何得到每个td的值。

 <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 - &gt;

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

(..等)

注意列宽为空...

1 个答案:

答案 0 :(得分: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>