XML,XSL和CSS中的表格变换

时间:2014-04-17 06:32:58

标签: css xml xslt

我的文件已经填满了要修改的数据。该查询与表有关。

这是我的文件.XML中的代码:

    <component>
        <section>   
            <text>
                <table>
                    <thead>
                        <tr>
                            <th>
                                Vermögen
                            </th>
                            <th>
                                Pflegediagnosen und Pflegeinterventionen
                            </th>
                        </tr>
                    </thead>        
                    <tbody>
                        <tr>
                            <td>
                            o selbstständig <br />
                            </td>
                            <td>rowspan down</td>
                        </tr>
                        <tr>
                            <td>o teilweise Unterstützung</td>
                        </tr>
                        <tr>
                            <td>o vollständige Unterstützung</td>
                        </tr>
                        <tr>
                            <td>o Kontrolle und Aufsicht</td>
                        </tr>
                    </tbody>
                </table>        
            </text>

以下是我的XSL预定义代码:

<!--
  table
  tfoot with colspan over rows
  even/odd rendering done with css3 because of new xELGA_red class on <tr>
   -->
  <xsl:template match="n1:table/@*|n1:thead/@*|n1:tfoot/@*|n1:tbody/@*|n1:colgroup/@*|n1:col/@*|n1:tr/@*|n1:th/@*|n1:td/@*">
    <xsl:copy>
    <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="n1:table">
  <xsl:variable name="numColumns">
        <xsl:value-of select="count(n1:thead/n1:tr/n1:th)"/>
    </xsl:variable>
    <table class="section_table" cellspacing="0" cellpadding="0"><!--  numColumns="{$numColumns}" -->
    <xsl:apply-templates>
      <xsl:with-param name="numColumns" select="$numColumns" />
    </xsl:apply-templates>
    </table> 
  </xsl:template>
  <xsl:template match="n1:thead">
    <thead>
      <xsl:apply-templates/>
    </thead>
  </xsl:template>
  <xsl:template match="n1:tfoot">
  <xsl:param name="numColumns"/>
    <tfoot>
        <xsl:apply-templates>
      <xsl:with-param name="numColumns" select="$numColumns" />
    </xsl:apply-templates>
    </tfoot>
  </xsl:template>
  <xsl:template match="n1:tbody">
    <tbody>
      <xsl:apply-templates/>
    </tbody>
  </xsl:template>
  <xsl:template match="n1:colgroup">
    <colgroup>
      <xsl:apply-templates/>
    </colgroup>
  </xsl:template>
  <xsl:template match="n1:col">
    <col>
      <xsl:apply-templates/>
    </col>
  </xsl:template>
  <xsl:template match="n1:tr[position() mod 2 = 1]">
  <xsl:param name="numColumns"/>
    <tr class="odd">
    <xsl:apply-templates>
      <xsl:with-param name="numColumns" select="$numColumns" />
    </xsl:apply-templates>
    </tr>
  </xsl:template>
  <xsl:template match="n1:tr">
    <xsl:param name="numColumns"/>
    <tr class="even">
    <xsl:apply-templates>
      <xsl:with-param name="numColumns" select="$numColumns" />
    </xsl:apply-templates>
    </tr>
  </xsl:template>

  <!-- table-heading processing -->
  <xsl:template match="n1:th">

   <!-- sum up all given widths -->
   <xsl:variable name="sum">
     <xsl:call-template name="sumgivenwidths">
       <xsl:with-param name="widths" select="../n1:th" />
     </xsl:call-template>
   </xsl:variable>

   <!-- calculate table width -->
   <xsl:variable name="tablewidth">
     <xsl:call-template name="calctablewidth">
       <xsl:with-param name="widths" select="../n1:th" />
       <xsl:with-param name="sumgivenwidths" select="$sum" />
     </xsl:call-template>
   </xsl:variable>

    <!-- width calculating -->
    <xsl:variable name="cwidth">
      <xsl:choose>
         <xsl:when test="@styleCode != ''">
      <xsl:choose>
        <xsl:when test="substring-after(@styleCode, ':') &lt; 0">
          <xsl:value-of select="substring-after(@styleCode, ':-')" />
        </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="substring-after(@styleCode, ':')" /> 
            </xsl:otherwise>
      </xsl:choose>

        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="(100 - $sum) div count(../n1:th[not(@styleCode != '')])" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- 100 percent scaling -->
    <xsl:variable name="scalewidth" select="concat($cwidth * 100 div $tablewidth, '%')" />

    <!-- create th element -->
    <xsl:element name="th">
      <xsl:attribute name="width"><xsl:value-of select="$scalewidth"/></xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- recursive loop through all given widths -->
  <xsl:template name="sumgivenwidths">
    <xsl:param name="widths" />
    <xsl:param name="sum" select="0" />

    <xsl:variable name="current" select="$widths[1]" />
    <xsl:variable name="next" select="$widths[position()>1]" />
    <xsl:variable name="currentwidth">
      <xsl:choose>
        <xsl:when test="substring-after($current/@styleCode,'xELGA_colw:') != ''">
          <!-- absolute value -->
          <xsl:choose>
        <xsl:when test="substring-after($current/@styleCode, 'xELGA_colw:') &lt; 0">
          <xsl:value-of select="substring-after($current/@styleCode, 'xELGA_colw:') * -1" />
            </xsl:when>
            <xsl:otherwise>
          <xsl:value-of select="substring-after($current/@styleCode, 'xELGA_colw:')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="0" />
        </xsl:otherwise> 
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="not($next)">
        <xsl:value-of select="$currentwidth + $sum" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="sumgivenwidths">
          <xsl:with-param name="widths" select="$next" />
          <xsl:with-param name="sum" select="$currentwidth + $sum" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- recursive calculation of table width -->
  <xsl:template name="calctablewidth">
    <xsl:param name="widths" />
    <xsl:param name="sumgivenwidths" />
    <xsl:param name="sum" select="0" />

    <xsl:variable name="current" select="$widths[1]" />
    <xsl:variable name="next" select="$widths[position()>1]" />
    <xsl:variable name="currentwidth">
      <xsl:choose>
        <xsl:when test="substring-after($current/@styleCode, 'xELGA_colw:') != ''">
          <!-- absolute value -->
          <xsl:choose>
            <xsl:when test="substring-after($current/@styleCode, 'xELGA_colw:') &lt; 0">
              <xsl:value-of select="substring-after($current/@styleCode, 'xELGA_colw:') * -1" />              
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="substring-after($current/@styleCode, 'xELGA_colw:')" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="(100 - $sumgivenwidths) div count(../n1:th[not(@styleCode != '')])" />
        </xsl:otherwise> 
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="not($next)">
        <xsl:value-of select="$currentwidth + $sum" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="calctablewidth">
          <xsl:with-param name="widths" select="$next" />
          <xsl:with-param name="sumgivenwidths" select="$sumgivenwidths" />
          <xsl:with-param name="sum" select="$currentwidth + $sum" />
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="n1:td">
  <!-- td-element ELGA stylecode processing -->
    <xsl:variable name="transform_smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="transform_uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

    <xsl:variable name="transformed_stylecode" select="translate(@styleCode, $transform_smallcase, $transform_uppercase)" />

    <xsl:variable name="tdStyleCode_Style">
      <xsl:if test="contains($transformed_stylecode, 'LRULE')">
        <xsl:text>text-align: left;</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'RRULE')">
        <xsl:text>text-align: right;</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'TOPRULE')">
        <xsl:text>vertical-align: top;</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'BOTRULE')">
        <xsl:text>vertical-align: bottom;</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'BOLD')">
        <xsl:text>font-weight: bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'UNDERLINE')">
        <xsl:text>text-decoration: underline</xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'ITALICS')">
        <xsl:text>font-style: italic;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="tdStyleCode_Class">
      <xsl:if test="contains($transformed_stylecode, 'EMPHASIS')">
        <xsl:text> smallcaps </xsl:text>
      </xsl:if>    
      <xsl:if test="contains($transformed_stylecode, 'XELGA_BLUE')">
        <xsl:text> xblue </xsl:text>
      </xsl:if>
      <xsl:if test="contains($transformed_stylecode, 'XELGA_RED')">
        <xsl:text> xred </xsl:text>
      </xsl:if>
    </xsl:variable>
    <td style="{$tdStyleCode_Style}" class="{$tdStyleCode_Class}">
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  <xsl:template match="n1:tfoot/*/n1:td">
  <xsl:param name="numColumns"/>
    <td colspan="{$numColumns}">
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  <xsl:template match="n1:table/n1:caption">
    <span class="caption">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

在我的XLS文件中还有一个CSS:

.section_table {
  border: 0.2em solid black;
  width: 100%;
}

.section_table td ul {
  margin-top: 0;
  margin-bottom: 0;
}

.section_table th {
  text-align: left;
  background-color: #F8D683;
  border-bottom: 0.2em solid white;
}

.section_table tr:nth-child(2n),
.section_table .even td {
  background-color: #FCF0D3;
}

.section_table th,.section_table td {
  padding: 0.3em;
}

正如你在CSS文件中看到的那样,表的宽度是100%, 现在我有2列,并希望将第一列的宽度设置为40%,将第二列的宽度设置为第二列。

的XML中
<td>rowspan down</td>

我想把行的行放下来。我不知道在哪里设置它。关于rowspan的所需输出:jsfiddle.net/U5ggX

我可能有更多表格,并且不希望将此设置提供给除此之外的任何表格。

任何人都可以帮助我。 非常感谢你提前!

问候, 马里奥

0 个答案:

没有答案