BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
The XSLT Example: Creating an HTML document with 'previous' and 'next' links
The XSLT Example: Generating a new stylesheet
The XSLT Example: Using different axes
The XSLT Example: Creating a summary of author sales for a publisher
The XSLT Example: Creating a summary of all Apollo flights
The XSLT Example:
The XSLT Element: xsl:with-param
The XSLT Element: xsl:when
The XSLT Element: xsl:variable
The XSLT Element: xsl:value-of
The XSLT Element: xsl:transform
The XSLT Element: xsl:text
The XSLT Element: xsl:template
The XSLT Element: xsl:stylesheet
The XSLT Element: xsl:strip-space
The XSLT Element: xsl:sort
The XSLT Element: xsl:processing-instruction
The XSLT Element: xsl:preserve-space
The XSLT Element: xsl:param
The XSLT Element: xsl:output
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :525

 
[XSLT Reference] [XPath Reference] [Download Reference Download this Reference]

Example: Creating listboxes and checkboxes using variables

Creating listboxes and sets of checkboxes from XML data is a recurring task. In these situations, often the possible values are stored at one location in the source document, while the chosen values (which must be represented by adding 'SELECTED' or 'CHECKED' to the corresponding item) are probably stored in another place or even another document.
In this example we use setting variables as a way to match stored values against possible values inside a for-each loop. The key element is used to have easy access to lists of possible values.
XSLT elements used:xsl:stylesheet xsl:template xsl:apply-templates xsl:value-of xsl:key xsl:output xsl:variable xsl:if xsl:for-each xsl:attribute
XSLT and XPath functions used:key concat
XML source:<?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 code:<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>
Resulting XML output:
saxon
xt
xalan
msxml3
msxml.NET
<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>An error occurred<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> <BR> <SELECT name="q_1"></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> <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> <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>

Note: You can now download the full content of the XPath reference, the XSLT reference and the DOM reference in one PDF document. This document contains the implementation tables, internal links to navigate through the references, a full linked table of contents. You can use free text search through the whole reference or print as a nicely formatted document. Download here...

© 2000 Teun Duynstee. Shown on TopXML.com. Information used from XSLT and XPath recommendations © W3C and MSDN documentation © Microsoft.
Do you have comments, additions or suggestions, mail me.

Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C

View all Jobs (Add yours)
View all CV (Add yours)



fax server
swimming pool contractor
teleconferencing
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP