zoukankan      html  css  js  c++  java
  • 在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>
  • 相关阅读:
    Android的Activity屏幕切换动画(一)-左右滑动切换
    404 Not Found 由来
    HTML+CSS 制作HTML5标志图
    发现 网站错误
    链接指南
    偷懒省事有工具啊
    程序员很穷(转)
    谷歌浏览器修改CSS和js后同步保存到文件中 (译)
    程序员眼睛的保护(爱护眼睛,你我做起)
    仿站违法和侵权吗?
  • 原文地址:https://www.cnblogs.com/goody9807/p/934402.html
Copyright © 2011-2022 走看看