<!--

# =============================================================================

# Copyright © 2013 Typéfi Systems. All rights reserved.

#

# Unless required by applicable law or agreed to in writing, software

# is distributed on an "as is" basis, without warranties or conditions of any

# kind, either express or implied.

# =============================================================================

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tps="http://www.typefi.com/ContentXML" xmlns:xd="http://www.pnp-software.com/XSLTdoc" version="2.0" exclude-result-prefixes="xs tps">





    
    <xd:doc type="stylesheet">

        
<xd:short>

            Converts tables to cxml

        
</xd:short>

        
<xd:detail>

            If the input table has Cals format then table is converted to cxml

            if the input table has XHTML format

            then starts conversion table to Cals format via "
<code>xhtml-to-cals</code>" mode

            (see see 
<a href="jats-tables-xhtml-to-cals.xsl.xd.html">jats-tables-xhtml-to-cals.xsl</a>)

            and result is processed as normal Cals table

        
</xd:detail>

        
<xd:cvsId>   $Revision$</xd:cvsId>

    
</xd:doc>








    
<xsl:template match="table-wrap">

        
<tps:context type="{$table-wrap.context.type}" id="{generate-id()}">

            
<xsl:copy-of select="tps:create-attribute('variant', tps:get-table-wrap-variant(.))" />

            
<xsl:apply-templates select="element() except label" />

        
</tps:context>

    
</xsl:template>





    
<xsl:function name="tps:get-table-wrap-variant" as="xs:string?">

        
<xsl:param name="table-wrap" as="element(table-wrap)" />



        
<xsl:variable name="mapped-variant" select="tps:get-mapped-value(tps:get-table-wrap-variant-input($table-wrap), $table-wrap.variant.mapping)" />



        
<xsl:sequence select="($mapped-variant[$table-wrap.variant.mapping.enabled], $table-wrap.variant.default)[1]" />

    
</xsl:function>





    
<xsl:function name="tps:get-table-wrap-variant-input" as="item()?">

        
<xsl:param name="table-wrap" as="element(table-wrap)" />



        
<xsl:sequence select="$table-wrap/processing-instruction('Table')" />

    
</xsl:function>





    
<xsl:template match="table-wrap/label">

        
<xsl:apply-templates />

        
<xsl:text></xsl:text>

    
</xsl:template>





    
<xsl:template match="table-wrap/caption[title]">

        
<tps:p type="{$table-wrap.caption.para.type}">

            
<xsl:copy-of select="parent::table-wrap/@id" />

            
<xsl:apply-templates select="../label" />

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/p[1]" priority="10">

        
<tps:p type="{$table-wrap-foot.p.first.para.type}">

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/p">

        
<tps:p type="{$table-wrap-foot.p.para.type}">

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/attrib">

        
<tps:p type="{$table-wrap-foot.attrib.para.type}">

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>

    

    

    
<xsl:template match="table-wrap-foot/fn[1]" priority="10">

        
<tps:p type="{$table-wrap-foot.fn.first.para.type}">

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/fn">

        
<tps:p type="{$table-wrap-foot.fn.para.type}">

            
<xsl:apply-templates />

        
</tps:p>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/fn/label">

        
<xsl:apply-templates />

        
<xsl:text></xsl:text>

    
</xsl:template>





    
<xsl:template match="table-wrap-foot/fn/p">

        
<xsl:apply-templates />

    
</xsl:template>





    
<xsl:template match="*:table[not(tps:is-cals-table(.))]" priority="10">

        
<xsl:variable name="cals-table" as="element(table)">

            
<xsl:apply-templates select="." mode="xhtml-to-cals" />

        
</xsl:variable>



        
<xsl:apply-templates select="$cals-table" />

    
</xsl:template>





    
<xsl:template match="*:table">

        
<tps:table type="{$table.type}">

            
<xsl:apply-templates />

        
</tps:table>

    
</xsl:template>





    
<!-- @Description:  CALS table entries elements are named "entry", whereas XHTML table entries

                        are marked as "td". 
-->

    
<xsl:function name="tps:is-cals-table" as="xs:boolean">

        
<xsl:param name="table" as="element()" />



        
<!-- Prevent infinite recursion on empty tables in tests -->

        
<xsl:sequence select="exists($table/tgroup) or exists($table/descendant::*:entry)" />

    
</xsl:function>





    
<xsl:template match="*:tgroup">

        
<tps:tgroup cols="{@cols}">

            
<xsl:call-template name="process-oasis-tgroup" />

        
</tps:tgroup>

    
</xsl:template>





    
<xsl:template name="process-oasis-tgroup">

        
<xsl:apply-templates />

    
</xsl:template>





    
<xsl:template match="*:colspec">

        
<tps:colspec colname="{@colnum}" colwidth="{@colwidth}">

            
<xsl:apply-templates />

        
</tps:colspec>

    
</xsl:template>





    
<xsl:template match="*:thead">

        
<tps:thead>

            
<xsl:apply-templates />

        
</tps:thead>

    
</xsl:template>





    
<xsl:template match="*:tbody">

        
<tps:tbody>

            
<xsl:apply-templates />

        
</tps:tbody>

    
</xsl:template>





    
<xsl:template match="*:tfoot">

        
<tps:tfoot>

            
<xsl:apply-templates />

        
</tps:tfoot>

    
</xsl:template>





    
<xsl:template match="*:row">

        
<tps:row>

            
<xsl:apply-templates />

        
</tps:row>

    
</xsl:template>





    
<xsl:template match="*:entry">

        
<xsl:variable name="align" select="tps:get-entry-alignment(.)" />



        
<tps:entry>

            
<xsl:apply-templates select="attribute()" />



            
<xsl:if test="exists($align)">

                
<xsl:attribute name="align" select="$align" />

            
</xsl:if>



            
<tps:p type="{tps:get-table-entry-para-type(.)}">

                
<xsl:apply-templates />

                
<xsl:call-template name="insert-entry-char" />

            
</tps:p>

        
</tps:entry>

    
</xsl:template>





    
<xsl:template name="insert-entry-char">

        
<xsl:variable name="alignment" select="(@char, '.')[1]" />



        
<xsl:if test="@align eq 'char'">

            
<tps:c type="{$table.entry.char.c.type}">

                
<xsl:value-of select="$alignment" />

            
</tps:c>

        
</xsl:if>

    
</xsl:template>





    
<xsl:template match="*:entry/@namest | *:entry/@nameend" priority="10">

        
<xsl:attribute name="{name()}" select="replace(., 'col', '')" />

    
</xsl:template>





    
<xsl:template match="*:entry/@valign | *:entry/@morerows" priority="10">

        
<xsl:attribute name="{name()}" select="." />

    
</xsl:template>





    
<xsl:template match="*:entry/attribute()" />





    
<xsl:function name="tps:get-entry-alignment" as="xs:string?">

        
<xsl:param name="entry" as="element()" />



        
<xsl:variable name="alignment" as="xs:string?">

            
<xsl:apply-templates select="$entry" mode="entry-alignment" />

        
</xsl:variable>



        
<xsl:sequence select="if ($alignment eq 'char') then $table.entry.char.alignment else $alignment" />

    
</xsl:function>





    
<xsl:template match="*:entry[@align]" mode="entry-alignment" priority="30">

        
<xsl:sequence select="@align" />

    
</xsl:template>





    
<xsl:template match="*:entry[exists(tps:get-colspec(.)/@align)]" mode="entry-alignment" priority="20">

        
<xsl:sequence select="tps:get-colspec(.)/@align" />

    
</xsl:template>





    
<xsl:template match="*:tgroup[@align]//*:entry" mode="entry-alignment" priority="10">

        
<xsl:sequence select="ancestor::*:tgroup/@align[1]" />

    
</xsl:template>





    
<xsl:template match="*:entry" mode="entry-alignment">

        
<xsl:sequence select="()" />

    
</xsl:template>





    
<xsl:function name="tps:get-colspec" as="element()?">

        
<xsl:param name="entry" as="element()" />



        
<xsl:variable name="table" select="$entry/ancestor::*:table[1]" />



        
<xsl:sequence select="$table//*:colspec[tps:get-column-number(.) eq tps:get-entry-column-number($entry)]" />

    
</xsl:function>





    
<xsl:function name="tps:get-column-number" as="xs:integer">

        
<xsl:param name="colspec" as="element()" />



        
<xsl:sequence select="($colspec/@colnum, tps:get-column-number-from-name($colspec/@colname), count($colspec/preceding-sibling::*:colspec) + 1)[1]" />

    
</xsl:function>





    
<xsl:function name="tps:get-entry-column-number" as="xs:integer">

        
<xsl:param name="entry" as="element()" />



        
<xsl:sequence select="sum(for $preceding-entry in $entry/preceding-sibling::*:entry return tps:get-entry-column-span($preceding-entry)) + 1" />

    
</xsl:function>





    
<xsl:function name="tps:get-entry-column-span" as="xs:integer">

        
<xsl:param name="entry" as="element()" />



        
<xsl:variable name="namest" select="$entry/@namest" as="attribute()?" />

        
<xsl:variable name="nameend" select="$entry/@nameend" as="attribute()?" />



        
<xsl:sequence select="if (empty($nameend)) then 1 else tps:get-column-number-from-name($nameend) - tps:get-column-number-from-name($namest) + 1" />

    
</xsl:function>





    
<xsl:function name="tps:get-column-number-from-name" as="xs:integer?">

        
<xsl:param name="name" as="node()?" />



        
<xsl:variable name="result" select="replace($name, '[A-Za-z-]+', '')" />



        
<xsl:sequence select="if ($result castable as xs:integer) then $result cast as xs:integer else ()" />

    
</xsl:function>





    
<xsl:function name="tps:get-table-entry-para-type" as="xs:string">

        
<xsl:param name="entry" as="element()" />



        
<xsl:variable name="result" as="xs:string*">

            
<xsl:apply-templates select="$entry" mode="get-table-entry-para-type" />

        
</xsl:variable>



        
<xsl:sequence select="string-join($result, '')" />

    
</xsl:function>





    
<xsl:template match="*:thead//*:entry" mode="get-table-entry-para-type" priority="10">

        
<xsl:sequence select="$table.thead.entry.para.type" />

    
</xsl:template>





    
<xsl:template match="*:tbody//*:entry" mode="get-table-entry-para-type" priority="10">

        
<xsl:sequence select="$table.tbody.entry.para.type" />

    
</xsl:template>





    
<xsl:template match="*:tfoot//*:entry" mode="get-table-entry-para-type" priority="10">

        
<xsl:sequence select="$table.tfoot.entry.para.type" />

    
</xsl:template>





    
<xsl:template match="*:entry" mode="get-table-entry-para-type">

        
<xsl:sequence select="$table.default.entry.para.type" />

    
</xsl:template>



</xsl:stylesheet>













































































v