XSLT
XSLT Contents
Summary This example utilizes variables and node lists being passed into a select/option XSLT template.
I have in the past used methods like: <SELECT ID=MySelectOption> <xsl:for-each select=CurrentNodeContext/NodeValues> <option><xsl:value-of select=MyOptionValue/></option> </xsl:for-each> </SELECT> or <SELECT ID=MySelectOption> <xsl:call-template select=CurrentNodeContext/NodeValues/> </SELECT> to render a HTML SELECT/Option tag using XSLT. Look familiar? Well here is yet another and in my opinion, a better one.
<SELECT ID=MySelectOption> <xsl:for-each select=CurrentNodeContext/NodeValues> <option><xsl:value-of select=MyOptionValue/></option> </xsl:for-each> </SELECT>
<SELECT ID=MySelectOption> <xsl:call-template select=CurrentNodeContext/NodeValues/> </SELECT>
What if you had to render the same HTML select/option list many, many times? Instead of XPath-ing the context nodes every time, do it once. With a single call, the programmer can pass in a variable containing the context node list (that is set globally) to a template. <xsl:variable name=USStates select=States/tStateLookup />
<xsl:variable name=USStates select=States/tStateLookup />
Optionally, the ability to set specific SELECT/Option attributes exists as well.
<xsl:value-of select=*[local-name() = $optionDisplay] />
The downloadable file contains a XML file and a XSL file with three types of SELECT/Option examples. Transforming the XML will render all three. In the following example I show a simple single select, single display Select/Option example.
Select Box - Style 1 Single Select Single Display The Caller - This example is one of three contained in the downloadable file. <xsl:call-template name=BuildUserSelectOptions> <xsl:with-param name=list select=$USStates/> <xsl:with-param name=optionID select='States_Style1'/> <xsl:with-param name=optionNAME select='States_Style1'/> <xsl:with-param name=optionSelected select=state/> <xsl:with-param name=optionValue select='State'/> <xsl:with-param name=optionDisplay select='State'/> <xsl:with-param name=optionOther1 select='StateName'/> <xsl:with-param name=optionOther2 select='Please select a state'/> </xsl:call-template> The Template <xsl:template name=BuildUserSelectOptions > <xsl:param name=list /> <xsl:param name=optionID /> <xsl:param name=optionNAME /> <xsl:param name=optionSelected /> <xsl:param name=optionValue /> <xsl:param name=optionDisplay /> <xsl:param name=optionOther1 /> <xsl:param name=optionOther2 /> <xsl:param name=optionOther3 /> <select name=Trustees onchange=DispSelectValue(this) > <option value=><xsl:value-of select=$optionOther2/></option> <xsl:for-each select=$list> <xsl:sort select=*[local-name() = $optionDisplay] order=ascending/> <xsl:element name=Option> <xsl:attribute name=value> <xsl:value-of select=*[local-name() = $optionValue]/> </xsl:attribute> <xsl:if test=$optionSelected=(*[local-name() = $optionValue])> <xsl:attribute name=SELECTED>TRUE</xsl:attribute> </xsl:if> <xsl:value-of select=*[local-name() = $optionDisplay] /> <xsl:text disable-output-escaping=yes> </xsl:text> - <xsl:text disable-output-escaping=yes> </xsl:text> (<xsl:value-of select=*[local-name() = $optionOther1]/>) </xsl:element> </xsl:for-each> </select> </xsl:template>
<xsl:call-template name=BuildUserSelectOptions> <xsl:with-param name=list select=$USStates/> <xsl:with-param name=optionID select='States_Style1'/> <xsl:with-param name=optionNAME select='States_Style1'/> <xsl:with-param name=optionSelected select=state/> <xsl:with-param name=optionValue select='State'/> <xsl:with-param name=optionDisplay select='State'/> <xsl:with-param name=optionOther1 select='StateName'/> <xsl:with-param name=optionOther2 select='Please select a state'/> </xsl:call-template>
<xsl:template name=BuildUserSelectOptions > <xsl:param name=list /> <xsl:param name=optionID /> <xsl:param name=optionNAME /> <xsl:param name=optionSelected /> <xsl:param name=optionValue /> <xsl:param name=optionDisplay /> <xsl:param name=optionOther1 /> <xsl:param name=optionOther2 /> <xsl:param name=optionOther3 /> <select name=Trustees onchange=DispSelectValue(this) > <option value=><xsl:value-of select=$optionOther2/></option> <xsl:for-each select=$list> <xsl:sort select=*[local-name() = $optionDisplay] order=ascending/> <xsl:element name=Option> <xsl:attribute name=value> <xsl:value-of select=*[local-name() = $optionValue]/> </xsl:attribute> <xsl:if test=$optionSelected=(*[local-name() = $optionValue])> <xsl:attribute name=SELECTED>TRUE</xsl:attribute> </xsl:if> <xsl:value-of select=*[local-name() = $optionDisplay] /> <xsl:text disable-output-escaping=yes> </xsl:text> - <xsl:text disable-output-escaping=yes> </xsl:text> (<xsl:value-of select=*[local-name() = $optionOther1]/>) </xsl:element> </xsl:for-each> </select> </xsl:template>
Partners
Dream.In.Code dotNet Slackers dotNet Spider Your HTML Source VisualBuilder.com DevGuru Planet Source Code ZVON.ORG Web Design ASPAlliance XML Pitstop Scripts
The Spot 4 SAP Bitshop Web Hosting