精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>XML>>(2)XML核心规范>>(2)XSL>>XSLT例子三:如何使用参数增加listboxes和checkboxes

主题:XSLT例子三:如何使用参数增加listboxes和checkboxes
发信人: mysunshine()
整理人: leitiger(2001-06-27 15:25:36), 站内信件
XSLT例子三:使用参数增加listboxes和checkboxes

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





----
$$      $$$$$$    
$$        $$    
$$        $$    
$$        $$     
$$        $$
$$$$$$  $$$      

[关闭][返回]