Changeset 70:032c62852ef1

Show
Ignore:
Timestamp:
01/07/12 19:39:28 (12 years ago)
Author:
František Kučera <franta-hg@…>
Branch:
default
Message:

Tabulky #15: data tabulky můžeme načítat i ze souboru + můžeme určit oddělovač.

Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • vstup/kontakt.xml

    r36 r70  
    5151                        Ale protože je to tajné, tak vám neřekneme, co A, B a C znamenají. 
    5252                </p> 
     53                 
     54                <h2>Telefonní seznam</h2> 
     55                 
     56                <p> 
     57                        Můžete nám kdykoli zavolat: 
     58                </p> 
     59                 
     60                <m:tabulka src="telefonní-seznam.csv"/> 
    5361 
    5462        </text> 
  • šablona/makra/tabulka.xsl

    r61 r70  
    1818--> 
    1919<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              
     20xmlns="http://www.w3.org/1999/xhtml" 
     21xmlns:m="https://trac.frantovo.cz/xml-web-generator/wiki/xmlns/makro" 
     22xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     23exclude-result-prefixes="m"> 
     24 
    2525        <!-- 
    2626                Jednoduché tabulky 
     
    3030                První řádek se považuje za záhlaví (nadpisy sloupců). 
    3131                * 
     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)  
    3234        --> 
    33     <xsl:template match="m:tabulka"> 
    34         <table> 
    35                 <xsl:variable name="data" select="replace(replace(text(), '^\s+', ''),'\s+$','')"/> 
    36                 <xsl:variable name="hlavička" select="substring-before($data, '&#10;')"/> 
    37                 <xsl:variable name="tělo" select="substring-after($data, '&#10;')"/> 
    38                 <thead> 
    39                         <tr> 
    40                                 <xsl:for-each select="tokenize($hlavička, '\t+')"> 
    41                                         <xsl:if test="normalize-space(.)"> 
     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        <xsl:template match="m:tabulka[@src]"> 
     43                <xsl:call-template name="vykresliTabulku"> 
     44                        <xsl:with-param name="zadání" select="unparsed-text(concat('../', $vstup, @src))"/> 
     45                        <xsl:with-param name="oddělovač" select="(@oddělovač, ';')[1]"/> 
     46                </xsl:call-template> 
     47        </xsl:template> 
     48         
     49        <xsl:template name="vykresliTabulku"> 
     50                <xsl:param name="zadání"/> 
     51                <xsl:param name="oddělovač"/> 
     52                <table> 
     53                        <xsl:variable name="data" select="replace(replace($zadání, '^\s+', ''),'\s+$','')"/> 
     54                        <xsl:variable name="hlavička" select="substring-before($data, '&#10;')"/> 
     55                        <xsl:variable name="tělo" select="substring-after($data, '&#10;')"/> 
     56                        <thead> 
     57                                <tr> 
     58                                        <xsl:for-each select="tokenize($hlavička, $oddělovač)"> 
     59                                                <xsl:if test="normalize-space(.)"> 
    4260                                                        <td><xsl:value-of select="normalize-space(.)"/></td> 
    4361                                                </xsl:if> 
    4462                                        </xsl:for-each> 
    45                         </tr> 
    46                 </thead> 
    47                 <tbody> 
     63                                </tr> 
     64                        </thead> 
     65                        <tbody> 
    4866                                <xsl:for-each select="tokenize($tělo, '\n')"> 
    4967                                        <xsl:if test="normalize-space(.)"> 
    5068                                                <tr> 
    51                                                         <xsl:for-each select="tokenize(., '\t+')"> 
     69                                                        <xsl:for-each select="tokenize(., $oddělovač)"> 
    5270                                                                <xsl:if test="normalize-space(.)"> 
    5371                                                                        <td><xsl:value-of select="normalize-space(.)"/></td> 
     
    5775                                        </xsl:if> 
    5876                                </xsl:for-each> 
    59                 </tbody> 
    60         </table>     
    61     </xsl:template> 
     77                        </tbody> 
     78                </table> 
     79        </xsl:template> 
    6280 
    6381</xsl:stylesheet>