`

XSLT例子三:使用参数增加listboxes和checkboxes

阅读更多


使用XSLT元素:xsl:stylesheetxsl:templatexsl:apply-templatesxsl:value-ofxsl:keyxsl:outputxsl:paramxsl:with-paramxsl:ifxsl:for-eachxsl:attribute
使用XSLT和Xpath的函数:keyconcat
XML源文件:
<?xml version="1.0"?>
<questionaire>
<questions>
<question id="1" listref="agree" style="listbox">
<text>Astheglobalsecondandthirdeventsin
orderofspectators,theWorldandEuropeanchampionships
soccerdeservemorecoverageoninternationalnewssites.a.CNN.com.
</text>
<value>4</value>
</question>
<question id="2" listref="colors" style="checkbox">
<text>Whatareyourfavoritecolors?</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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics