BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


XSLT
XSLT Date filtering, sorting, comparing, and evaluating
XSLT Template for HTML INPUT Tag
XSLT Template for HTML IMG Tag
XSLT Search and Replace Template
Word wrap and text replace templates for XSLT
nxslt 1.3
Visual XPath
xframe - xsddoc
-xsl-fo-->-pdf--making-an-e-book/documentation.aspx>BIBLE PDF from (xml & xsl)-> xsl-fo -> pdf ; Making an E-book/documentation
Sum(round(.))
DISTINCT list of attributes using XPATH only
How to add a XSL information to the XML file with the XmlTextWriter?
How to update an XML file with XmlDocument?
How to select specific nodes in XmlDocument()?
How to use GetElementsByTagName in XmlDocument?
How to Import a Node with XmlDocument and ImportNode?
How to Clone a Node with XmlDocument and CloneNode?
How to use XPath correctly?
How to add Processing Information with XmlDocument?
How to sort an XML using XPathExpression and AddSort?


 
 

<< XQuery.NET and XML >>


By Mike DiRenzo
First Posted 10/14/2003
Times viewed 1090

XSLT Select Option Example


This post contains attachments
v20031014101137.zip 

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.

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 />

Optionally, the ability to set specific SELECT/Option attributes exists as well.

  • ID
  • NAME
  • SELECTED Value
  • MULTI
  • SIZE
  • ONCLICK, ONCHANGE events
  • You get the idea....
But how do you dynamically pass in the NODE names contained in the NODE list that you want to have rendered? It is an age-old XSLT dilemma.
To have the NAME of the node instead of its VALUE passed in to the template as a string is tricky. You dont want to render the VALUE of the NODE but you do want code-generate the NAME of it. Sound confusing?
Here is the magic:
<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>&nbsp;</xsl:text>
         -
         <xsl:text disable-output-escaping=yes>&nbsp;</xsl:text>
         (<xsl:value-of select=*[local-name() = $optionOther1]/>)
       </xsl:element>
     </xsl:for-each>
   </select>
</xsl:template>


Rate this article on a scale of 1 to 10 (1 votes, average 5)

Your vote :  

<< XQuery.NET and XML >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML