在Xslt中如何创建元素(Element)和属性(attribute)

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>

xsl:attribute 在处理的同时生成元素。它在其包含的元素内创建属性

XSLT stylesheet 1

XML源码
<source>

<color>blue</color>
<color>navy</color>
<color>green</color>
<color>lime</color>
<color>red</color>

</source>

输出
<TABLE>
  <TR>
     <TD style="color:blue">blue</TD>
  </TR>
</TABLE>

<TABLE>
  <TR>
     <TD style="color:navy">navy</TD>
  </TR>
</TABLE>

<TABLE>
  <TR>
     <TD style="color:green">green</TD>
  </TR>
</TABLE>

<TABLE>
  <TR>
     <TD style="color:lime">lime</TD>
  </TR>
</TABLE>

<TABLE>
  <TR>
     <TD style="color:red">red</TD>
  </TR>
</TABLE>

用HTML察看
blue
navy
green
lime
red
XSLT stylesheet
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match="color">
     <TABLE>
          <TR>
               <TD>
                    <xsl:attribute name="style">
                         <xsl:text>color:</xsl:text>
                         <xsl:value-of select="."/>
                    </xsl:attribute>
                    <xsl:value-of select="."/>
               </TD>
          </TR>
     </TABLE>
</xsl:template>


</xsl:stylesheet>
posted @ 2007-10-23 13:12  PointNet  阅读(1385)  评论(0编辑  收藏  举报