root/šablona/makra/tabulka.xsl @ 111:d59023a42d4b

Revision 111:d59023a42d4b, 3.8 KB (checked in by František Kučera <franta-hg@…>, 12 years ago)

#20 Skriptování: vnořování maker – zadání tabulky nebo diagramu může být generované skriptem.

Line 
1<?xml version="1.0" encoding="UTF-8"?>
2<!--
3XML Web generátor – program na generování webových stránek
4Copyright © 2012 František Kučera (frantovo.cz)
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program.  If not, see <http://www.gnu.org/licenses/>.
18-->
19<xsl:stylesheet version="2.0"
20        xmlns="http://www.w3.org/1999/xhtml"
21        xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro"
22        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
23        exclude-result-prefixes="m">
24
25        <!--
26                Jednoduché tabulky
27                ******************
28                Data zadáváme do těla elementu.
29                Sloupce oddělujeme tabulátorem (může jich být víc, minimálně však jeden, mezery nestačí).
30                První řádek se považuje za záhlaví (nadpisy sloupců).
31                *
32                @src volitelně můžeme data tabulky načítat ze souboru
33                @oddělovač regulární výraz, který odděluje sloupce – např. „\t+“ pro tabulátory (výchozí pro tabulky vložené přímo do stránky) nebo „;“ pro středník (výchozí pro tabulky načítané ze souboru)
34        -->
35        <xsl:template match="m:tabulka">
36                <xsl:call-template name="vykresliTabulku">
37                        <xsl:with-param name="zadání" select="text()"/>
38                        <xsl:with-param name="oddělovač" select="(@oddělovač, '\t+')[1]"/>
39                </xsl:call-template>
40        </xsl:template>
41       
42        <!-- Skript je potřeba interpretovat ještě před tabulkou – ostatní makra budou interpretovat uvnitř buněk -->
43        <xsl:template match="m:tabulka[m:skript]">
44                <xsl:variable name="zadání">
45                        <xsl:apply-templates select="*"/>
46                </xsl:variable>
47                <xsl:call-template name="vykresliTabulku">
48                        <xsl:with-param name="zadání" select="$zadání"/>
49                        <xsl:with-param name="oddělovač" select="(@oddělovač, '\t+')[1]"/>
50                </xsl:call-template>
51        </xsl:template>
52       
53        <!-- Tabulka načítaná ze souboru: -->
54        <xsl:template match="m:tabulka[@src]">
55                <xsl:call-template name="vykresliTabulku">
56                        <xsl:with-param name="zadání" select="m:načti-textový-soubor(@src)"/>
57                        <xsl:with-param name="oddělovač" select="(@oddělovač, ';')[1]"/>
58                </xsl:call-template>
59        </xsl:template>
60       
61        <!-- TODO: Umožnit použití maker a značek uvnitř buněk tabulky. -->
62        <xsl:template name="vykresliTabulku">
63                <xsl:param name="zadání"/>
64                <xsl:param name="oddělovač"/>
65                <table>
66                        <xsl:variable name="data" select="replace(replace($zadání, '^\s+', ''),'\s+$','')"/>
67                        <xsl:variable name="hlavička" select="substring-before($data, '&#10;')"/>
68                        <xsl:variable name="tělo" select="substring-after($data, '&#10;')"/>
69                        <thead>
70                                <tr>
71                                        <xsl:for-each select="tokenize($hlavička, $oddělovač)">
72                                                <xsl:if test="normalize-space(.)">
73                                                        <td><xsl:value-of select="normalize-space(.)"/></td>
74                                                </xsl:if>
75                                        </xsl:for-each>
76                                </tr>
77                        </thead>
78                        <tbody>
79                                <xsl:for-each select="tokenize($tělo, '\n')">
80                                        <xsl:if test="normalize-space(.)">
81                                                <tr>
82                                                        <xsl:for-each select="tokenize(., $oddělovač)">
83                                                                <xsl:if test="normalize-space(.)">
84                                                                        <xsl:element name="td">
85                                                                                <xsl:if test="number(normalize-space(.))">
86                                                                                        <xsl:attribute name="class">číslo</xsl:attribute>
87                                                                                </xsl:if>
88                                                                                <xsl:value-of select="normalize-space(.)"/>
89                                                                        </xsl:element>
90                                                                </xsl:if>
91                                                        </xsl:for-each>
92                                                </tr>
93                                        </xsl:if>
94                                </xsl:for-each>
95                        </tbody>
96                </table>
97        </xsl:template>
98
99</xsl:stylesheet>
100
Note: See TracBrowser for help on using the browser.