XSLT专业站 - 提供xslt和xml相关的资料书籍和教程  
XSLT专业站 - 提供xslt和xml相关的资料书籍和教程
网站地图  收藏本站
首页 | 热门文章 | 精彩实例 | 经典教程 | XSLT语法详解 | 资料下载 | 休闲天地 | 交流论坛
  当前位置:首页>精彩实例>文章内容
XSLT 实例:使用变量(Variable)创建单选框和复选框
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-10-27  
XSLT 实例:使用变量(Variable)创建单选框和复选框
使用到的XSLT元素:   xsl:stylesheet xsl:template xsl:apply-templates xsl:value-of xsl:key xsl:output xsl:variable xsl:if xsl:for-each xsl:attribute
使用到的XSLT/XPath函数:   key concat

XML源文件:
<?xml version="1.0"?>
<questionaire> 
    <questions>
        <question id="1" listref="agree" style="listbox">
                <text>As the global second and third events in
                order of spectators, the World and European championships
                soccer deserve more coverage on international news site s.a. CNN.com.
                </text>
                <value>4</value>
        </question>
        <question id="2" listref="colors" style="checkbox">
                <text>What are your favorite colors?</text>
                <value>2</value>
                <value>4</value>
        </question>
    </questions>
    <answer-lists>
        <list name="colors">
            <option id="1" name="red" />
            <option id="2" name="yellow" />
            <option id="3" name="green" />
            <option id="4" name="red" />
            <option id="5" name="red" />
        </list>
        <list name="agree">
            <option id="1" name="strongly disagree" />
            <option id="2" name="disagree" />
            <option id="3" name="not sure" />
            <option id="4" name="agree" />
            <option id="5" name="strongly agree" />
        </list>
    </answer-lists>
</questionaire> 

XSLT源代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="lists" match="//list" use="attribute::name"/>
    <xsl:output method="html" indent="yes" encoding="ISO-8859-1"/>
    <xsl:template match="/">
        <HTML>
            <HEAD/>
            <BODY>
            <FORM>
                <xsl:apply-templates select="questionaire/questions/question"/>
            </FORM>
            </BODY>
        </HTML>
    </xsl:template>

    <xsl:template match="question">
        <P>
        <xsl:variable name="selected-values" select="value"/>
        <xsl:variable name="style" select="@style"/>
        <xsl:variable name="question_id" select="concat('q_',@id)"/>

        <xsl:value-of select="child::text/text()"/>
        <xsl:for-each select="key('lists', @listref)">
            <xsl:if test="$style='checkbox'">
                <xsl:for-each select="option">
                    <BR/>
                    <INPUT TYPE="checkbox" >
                        <xsl:attribute name="name">
                            <xsl:value-of select="$question_id"/>
                        </xsl:attribute>
                        <xsl:if test="$selected-values/text() = attribute::id">
                            <xsl:attribute name="CHECKED"/>
                        </xsl:if>
                    </INPUT>
                    <xsl:value-of select="@name"/>
                </xsl:for-each>

            </xsl:if>

            <xsl:if test="$style='listbox'">
              <BR/>
              <SELECT >
                <xsl:attribute name="name">
                    <xsl:value-of select="$question_id"/>
                </xsl:attribute>
                <xsl:for-each select="option">
                    <OPTION>
                        <xsl:if test="$selected-values/text() = attribute::id">
                            <xsl:attribute name="SELECTED"/>
                        </xsl:if>
                        <xsl:attribute name="value">
                            <xsl:value-of select="@id"/>
                        </xsl:attribute>
                        <xsl:value-of select="@name"/>
                    </OPTION>
                </xsl:for-each>
              </SELECT>

            </xsl:if>
       
        </xsl:for-each>
       
        </P>
    </xsl:template>

</xsl:stylesheet>

输出结果:
<HTML>
   <HEAD>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   </HEAD>
   <BODY>
      <FORM>
         <P>As the global second and third events in
            order of spectators, the World and European championships
            soccer deserve more coverage on international news site s.a. CNN.com.
                            <BR><SELECT name="q_1">
               <OPTION value="1">strongly disagree</OPTION>
               <OPTION value="2">disagree</OPTION>
               <OPTION value="3">not sure</OPTION>
               <OPTION SELECTED="" value="4">agree</OPTION>
               <OPTION value="5">strongly agree</OPTION></SELECT></P>
         <P>What are your favorite colors?<BR><INPUT TYPE="checkbox" name="q_2">red<BR><INPUT TYPE="checkbox" name="q_2" CHECKED="">yellow<BR><INPUT TYPE="checkbox" name="q_2">green<BR><INPUT TYPE="checkbox" name="q_2" CHECKED="">red<BR><INPUT TYPE="checkbox" name="q_2">red
         </P>
      </FORM>
   </BODY>
</HTML>


标题: XSLT 实例:使用变量(Variable)创建单选框和复选框
关键字:key concat xsl:stylesheet xsl:template xsl:apply-templates
上一篇:XSLT 实例:创建带有“前一页”和“后一页”的 HTML 文档   下一篇:没有了
  精彩实例热点文章
·XSLT 实例:使用不同的轴(Axis
·查找公共祖先节点
·利用 xslt 对 xml 进行缩进格式
·XSLT 实例:创建带有“前一页”
·利用 XSL 解析 RSS
·把数据库输表用 XSLT 转化为树状
·查找节点的父节点
·用 XML/XSL 来生成动态页面
·利用 XSL 对 XML 数据进行加密和
·XSLT 实例:统计作者出版物的销
  精彩实例相关文章
·XSLT 实例:创建带有“前一页”
·XSLT 实例:统计作者出版物的销
·XSLT 实例:使用不同的轴(Axis
·利用 XSL 对 XML 数据进行加密和
·利用 xslt 对 xml 进行缩进格式
·用 XML/XSL 来生成动态页面
·查找公共祖先节点
·查找节点的父节点
·把数据库输表用 XSLT 转化为树状
·利用 XSL 解析 RSS
Copyright© 2007 xslt.org.cn All rights reserved.