Help with displaying HTML controls using XML/XSL

Posted on 2004-09-09
Last Modified: 2013-11-19
Good afternoon,

I am relatively new to XML and XSL and am trying to create an XML data island that is transformed using XSL and JavaScript.  In my practice pages, I get a recordset back from a database and I use server side code to generate the XML for it and write it to the web page.  Then I have an XSL file that has the table layout and such.  Then I use JavaScript to load and display it.

My dummy example is like this:
<XML id="XMLData">

<XML id="tableLayout" src="Table.xsl"></XML>

In my XSL file (Table.xsl) it is basic and looks like:
<xsl:template match="record">
<TD><xsl:value-of select="id" /></TD>
<TD><xsl:value-of select="name"/></TD>
etc. etc.

However, now that I have a conceptual grasp of it, it's time to put it to use and I am having problems.  I want to convert some current ASP code to this data island concept. However, I have no idea where to begin when a cell contains an HTML control, like a checkbox, with logic associated with it.  Let's pretend that the table column before "ID" would contain a checkbox.  For instance, this is an IF statement, which is contained in a Do Loop for each record in the recordset:

If displayStatus ="clearAll" or displayStatus=""Then%>

<input type="checkbox" name="checkbox" value="<%=tmpFileNo%>">

<input type="checkbox" name="checkbox" checked value="<%=tmpFileNo%>" strCanCheckOut="True">
<%End If%>

You see above that depending on the value of displayStatus, a checkbox is included in this cell with different properties set by server variables too.  I don't know how to account for this in the XML or XSL.  How would I generate the XML for the situation above and how would I account for this in the XSL?  

*Please be detailed as possible* because I am very new to this and am struggling a bit.  Thanks for your help.

Question by:dentyne
  • 3
  • 2
LVL 15

Accepted Solution

dualsoul earned 225 total points
ID: 12024183
you can use stylesheet parameter and conditional logic in XSL to achive it, i'll show you an example:

ok, let's provide parameter to XSLT, like displayStatus:

it can be done:
<xsl:param name="displayStatus"/>

then, you can refer to it like this: $displayStatus,

and now see complete stylesheet similiar to your ASP page:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:param name="displayStatus"/>
      <xsl:param name="fileNo"/>
      <xsl:template match="/">
            <!-- test value of dipslayStatus -->
                  <!-- if(displayStatus=clear) -->
                  <xsl:when test="$displayStatus='clear'">
                        <input type="checkbox" name="checkbox" value="$fileNo"/>
                  <!-- else -->
                        <input type="checkbox" name="checkbox" checked="true" value="$fileNo"/>

Feel free to ask.
LVL 21

Expert Comment

ID: 12025093
Another way to handle conditional logic is with <xsl:if> and <xsl:attribute>.  This avoids repeating <input> elements multiple times
<input type='checkbox' name='checkbox{position()} value='{$fileNo}>
  <xsl:if test='$displayStatus!="clear"'>
     <xsl:attribute name='checked'>true</xsl:attribute>

Also note the {} characters.  They indictate to the XSL parser to evalute the XPATH expression (simalar to <%=..%> in ASP).  They were missing in dualsoul's example.

The code fragment above is using the position() function to get the position in a nodelist (i.e. part of a for-each loop).  It would produce output like this:
<input type='checkbox' name='checkbox1' value='1'/>
<input type='checkbox' name='checkbox2' value='1'/>
<input type='checkbox' name='checkbox3' value='1' checked='true'/>
<input type='checkbox' name='checkbox4' value='1'/>
<input type='checkbox' name='checkbox5' value='1'/>
LVL 15

Expert Comment

ID: 12026058
>Also note the {} characters.

oh yeah, exactly - i've missed it, it's called AVT - attribute value template - this is shortcut for <xsl:attribute> when no any logic during processing needed.

like this :
<input value="{$fileNo}" /> is equas to <xsl:attribute name="value" select="$fileNo" />
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.


Author Comment

ID: 12043674
Good morning!  Thanks for the responses.  I just got into the office and I will definitely be looking over these soon.  I just wanted to post a comment to say thanks and that I was just getting around to reading them.  

Author Comment

ID: 12044185
I am upping the point value 100 more points because I feel I might need a little bit more help.  I appreciate your time.  Thanks for both examples.  But how do the XSL parameters work?  Are they actually reading from the same parameter in the ASP page?  I did a small test creating the displayStatus variable in the ASP and setting it to "clear". But the logic in the XSLT didn't recognize this.  So I'm assuming I have to somehow populate the XSL parameter from my ASP page in the ASP do loop?  

The do loop I'm referring to in the ASP code is the loop that processes the recordset from the server.  The current, non-XML code, grabs the recordset and evaluates it row by row and based on some field values, populates variables like "displayStatus" and "fileNo".  Then the variables are evaluated at the HTML table row written with different values, such as a checkbox being unchecked or checked.

So in the new XML code, when I generate the XML, I am assuming I have to populate the XSL parameter that I created.  If I am correct in this assumption, how would I do this in my new XML generation loop? In the "new" XML code, here is what my XML generation looks like:

Response.Write("<xml id=""doclist"">" & vbCRLF)
Response.Write("<documents>" & vbCRLF)
Do While Not rs.EOF
   Response.Write("<record>" & vbCRLF)
   For x = 0 to rs.Fields.Count-1
      Response.Write("<" & lcase(rs.Fields(x).Name) & ">")
      Response.Write("</" & lcase(rs.Fields(x).Name) & ">" & vbCRLF)
   Response.Write("</record>" & vbCRLF)
Response.Write("</documents>" & vbCRLF)

The result would look like the XML in my original post. In the current, non-XML code, variables are populated in that Do Loop and HTML is written based on these values. So I assume this is where I would populate these XSL variables?

Thank you for your patience!

Author Comment

ID: 12054971
Hmm do they not have assisted answer points anymore?  

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question