xsl cals tables: span cells, using colspec, namest and nameend

眉间皱痕 提交于 2019-12-07 18:50:48

问题


I'm getting crazy with spanning cells of a table. My table is of 3 columns. Below you see one row (only fragment):

<tgroup>
    <colspec name="x">
    <colspec name="y">
    <colspec name="z">
    <tbody>
        <row>
        <entry>abc
        <entry namest="y" nameend="z">blabla

The second entry (blabla) should span two entry (=table cells). The information is in the attributes "nameend" and "namest".

My way is:

  1. xsl:template match="entry" ...

  2. get the position number of <colspec name=z> (here 3) and of <colspec name="y"> (here 2) <??????>

  3. substract z and y (=1) add 1: result=3

      <xsl:param name="colspan">
          <xsl:value-of select="($nameend)-($namest)+(1)"/>
      </xsl:param>
    
  4. use the result=3 as attribute "colspan" in the entry template

    <fo:table-cell number-columns-spanned="{$colspan}" 
    

But I see no way to solve my second step (????)

Any ideas?? Thanks Pia

P.S. No, I can not change the source file


回答1:


Given this input XML:

<?xml version="1.0" encoding="UTF-8"?>
<tgroup>
  <colspec name="x"/>
  <colspec name="y"/>
  <colspec name="z"/>
  <tbody>
    <row>
      <entry>abc</entry>
      <entry namest="y" nameend="z">blabla 1</entry>
    </row>
    <row>
      <entry namest="x" nameend="z">blabla 2</entry>
    </row>
    <row>
      <entry namest="x" nameend="y">blabla 3</entry>
      <entry>cde</entry>
    </row>
  </tbody>
</tgroup>

This XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="entry[@namest and @nameend]">
    <xsl:variable name="namest" select="@namest"/>
    <xsl:variable name="nameend" select="@nameend"/>
    <xsl:variable name="namestPos" select="count(ancestor::tgroup/colspec[@name=$namest]/preceding-sibling::colspec)"/>
    <xsl:variable name="nameendPos" select="count(ancestor::tgroup/colspec[@name=$nameend]/preceding-sibling::colspec)"/>

    <table-cell number-columns-spanned="{$nameendPos - $namestPos + 1}">
      <xsl:apply-templates/>
    </table-cell>
  </xsl:template>
</xsl:stylesheet>

Will yield this output:

<?xml version="1.0" encoding="UTF-8"?>
<tgroup>
  <colspec name="x"/>
  <colspec name="y"/>
  <colspec name="z"/>
  <tbody>
      <row>
         <entry>abc</entry>
         <table-cell number-columns-spanned="2">blabla 1</table-cell>
      </row>
      <row>
         <table-cell number-columns-spanned="3">blabla 2</table-cell>
      </row>
      <row>
         <table-cell number-columns-spanned="2">blabla 3</table-cell>
         <entry>cde</entry>
      </row>
  </tbody>
</tgroup>

Notes:

  • Although you wrote, "subtract z and y (=1) add 1: result=3", I assume that you meant "result=2".
  • entry is mapped to table-cell with the desired @number-columns-spanned attribute value, which is the key aspect of the question. Remapping of the surrounding elements and mapping to the fo namespace too remain to do.


来源:https://stackoverflow.com/questions/20408261/xsl-cals-tables-span-cells-using-colspec-namest-and-nameend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!