xslt

单选框和复选框
使用到的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元素:xsl:stylesheet   xsl:template   xsl:apply-templates   xsl:value-of   xsl:key   xsl:output   xsl:param   xsl:with-param   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="xml" indent="yes" omit-xml-declaration="yes"/>
    <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:value-of select="child::text/text()"/>
        <xsl:apply-templates select="key('lists', @listref)">
            <xsl:with-param name="selected-values" select="value"/>
            <xsl:with-param name="style" select="@style"/>
            <xsl:with-param name="question_id" select="concat('q_',@id)"/>
        </xsl:apply-templates>
        
        </P>
    </xsl:template>
    <xsl:template match="list">
        <xsl:param name="selected-values"/>
        <xsl:param name="style">listbox</xsl:param>
        <xsl:param name="question_id"/>
        
        <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:template>
</xsl:stylesheet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值