English | Français | Deutsch | Magyar | >> 中文 << | Polski ZVON > Tutorials > XSLT Tutorial
>> 页 22 << | 上一条 | 下一条 | 目录 | 元素索引

xsl:element 在处理的同时生成元素。 XSLT stylesheet 1 利用了这个特性,而 XSLT stylesheet 2 用另外一种劳神费力的方法达到同样的效果

XSLT stylesheet 1

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>

输出
<H1>Header1</H1>
<H3>Header3</H3>
<b>Bold text</b>
<sub>Subscript</sub>
<sup>Superscript</sup>

用HTML察看

Header1

Header3

Bold text Subscript Superscript
XSLT stylesheet
<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>


XSLT stylesheet 2

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>

输出
<H1>Header1</H1>
<H3>Header3</H3>
<b>Bold text</b>
<sub>Subscript</sub>
<sup>Superscript</sup>

用HTML察看

Header1

Header3

Bold text Subscript Superscript
XSLT stylesheet
<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>