使用变量(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>
posted @   架构师聊技术  阅读(313)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示