Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with displaying HTML controls using XML/XSL

Posted on 2004-09-09
6
Medium Priority
?
421 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 900 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

782 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