Solved

Help with displaying HTML controls using XML/XSL

Posted on 2004-09-09
6
405 Views
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">
<documents>
<record>
<id>12345</id>
<name>jacob</name>
</record>
<record>
<id>6789</id>
<name>paul</name>
</record>
</documents>
</XML>

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

In my XSL file (Table.xsl) it is basic and looks like:
<xsl:template match="record">
<TABLE>
<TR>
<TD><xsl:value-of select="id" /></TD>
<TD><xsl:value-of select="name"/></TD>
</TR>
</TABLE>
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%>">

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

Jacob
0
Comment
Question by:dentyne
  • 3
  • 2
6 Comments
 
LVL 15

Accepted Solution

by:
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="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 -->
            <xsl:choose>
                  <!-- if(displayStatus=clear) -->
                  <xsl:when test="$displayStatus='clear'">
                        <input type="checkbox" name="checkbox" value="$fileNo"/>
                  </xsl:when>
                  <!-- else -->
                  <xsl:otherwise>
                        <input type="checkbox" name="checkbox" checked="true" value="$fileNo"/>
                  </xsl:otherwise>
            </xsl:choose>
      </xsl:template>
</xsl:stylesheet>
............................................................

Feel free to ask.
0
 
LVL 21

Expert Comment

by:MogalManic
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>
  </xsl:if>
</input>
...

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'/>
...
0
 
LVL 15

Expert Comment

by:dualsoul
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" />
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:dentyne
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.  
0
 
LVL 1

Author Comment

by:dentyne
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(Server.HTMLEncode(Trim(Cstr(Null2Empty(rs.Fields(x).Value)))))
      Response.Write("</" & lcase(rs.Fields(x).Name) & ">" & vbCRLF)
   Next
   Response.Write("</record>" & vbCRLF)
   rs.MoveNext
Loop
Response.Write("</documents>" & vbCRLF)
Response.Write("</xml>")
---------------------

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!
0
 
LVL 1

Author Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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 dynamically set the form action using jQuery.
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.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now