English | Français | Deutsch | Magyar | 中文 | >> Polski << ZVON > Tutorials > XSLT Tutorial
>> Strona 22 << | Poprzedni | Następny | Zawartość | Indeks elementu

xsl:element tworzy elementy w trakcie przetwarzania. Arkusz stylów XSLT 1 pokazuje sposób użycia, podczas gdy Arkusz stylów XSLT 2 uzyskuje ten sam efekt używając jednak bardziej zawiłej metody.

Arkusz stylów XSLT 1

Źródło XML
<source>

<text size="H1">Header1</text>
<text size="H3">Header3</text>
<text size="b">Bold text</text>
<text size="sub">Subscript</text>
<text size="sup">Superscript</text>

</source>

Dane wyjściowe
<H1>Header1</H1>
<H3>Header3</H3>
<b>Bold text</b>
<sub>Subscript</sub>
<sup>Superscript</sup>

Widok HTML

Header1

Header3

Bold text Subscript Superscript
Arkusz stylów XSLT
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <xsl:for-each select="//text">
          <xsl:element name="{@size}">
               <xsl:value-of select="."/>
          </xsl:element>
     </xsl:for-each>
</xsl:template>


</xsl:stylesheet>


Arkusz stylów XSLT 2

Źródło XML
<source>

<text size="H1">Header1</text>
<text size="H3">Header3</text>
<text size="b">Bold text</text>
<text size="sub">Subscript</text>
<text size="sup">Superscript</text>

</source>

Dane wyjściowe
<H1>Header1</H1>
<H3>Header3</H3>
<b>Bold text</b>
<sub>Subscript</sub>
<sup>Superscript</sup>

Widok HTML

Header1

Header3

Bold text Subscript Superscript
Arkusz stylów XSLT
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="/">
     <xsl:for-each select="//text">
          <xsl:choose>
               <xsl:when test='@size="H1"'>
                    <H1>
                         <xsl:value-of select="."/>
                    </H1>
               </xsl:when>
               <xsl:when test='@size="H3"'>
                    <H3>
                         <xsl:value-of select="."/>
                    </H3>
               </xsl:when>
               <xsl:when test='@size="b"'>
                    <b>
                         <xsl:value-of select="."/>
                    </b>
               </xsl:when>
               <xsl:when test='@size="sub"'>
                    <sub>
                         <xsl:value-of select="."/>
                    </sub>
               </xsl:when>
               <xsl:when test='@size="sup"'>
                    <sup>
                         <xsl:value-of select="."/>
                    </sup>
               </xsl:when>
          </xsl:choose>
     </xsl:for-each>
</xsl:template>


</xsl:stylesheet>