• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

Help with displaying HTML controls using XML/XSL

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

  • 3
  • 2
1 Solution
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="http://www.w3.org/1999/XSL/Transform">
      <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.
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'/>
>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" />
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

dentyneAuthor Commented:
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.  
dentyneAuthor Commented:
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!
dentyneAuthor Commented:
Hmm do they not have assisted answer points anymore?  
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now