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

自古美人都是妖i 提交于 2019-12-06 12:32:54

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