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>
  • 相关阅读:
    hibernate4 无法保存 不报错
    win10开启mongodb 开启服务
    nodejs学习笔记
    mariadb Too many connections
    新老ECS数据库速度对比
    数据库自动备份并打成tar.gz包
    字符串和数组----string
    I/O复用(select)——回声服务器端/客户端
    回声UDP服务器端/客户端
    回声TCP服务器端/客户端
  • 原文地址:https://www.cnblogs.com/goody9807/p/934402.html
Copyright © 2011-2022 走看看