0
点赞
收藏
分享

微信扫一扫

使用变量(Variable)创建单选框和复选框

夏木之下 2022-07-28 阅读 102

单选框和复选框

使用到的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>



举报

相关推荐

0 条评论