XSL Elements
Let's take a look at some of the most frequently used XSL elements.
XSL Value-of
Value-of is previous section:
<xsl:value-of select="d:propstat/d:prop/category" />
XSL If
As you can guess by the name, the If element implements simple, conditional logic. You can pass in the Language parameter for this element to indicate the scripting language you want to use to evaluate script in your condition testing. The Test parameter is the actual condition you want to test for. The following code checks to see whether the subject of the item returned is not empty. You can test for multiple conditions by using the Choose, When, and Otherwise elements (discussed next).
<xsl:if test="d:propstat/d:prop/subject[.!='']">
XSL Choose, When, and Otherwise
You use the Choose, When, and Otherwise elements together when you require more complex conditional testing. You can use these three elements to implement an If…ElseIf...Else structure. The following code checks to see whether the start time of the course is less than the current time, which would mean the course has already taken place. If the start time is after the current time, a registration link is created for the course.
<xsl:choose>
<xsl:when test="d:propstat/d:prop/starttime[. <
'<%=TurnIntoIso(Date(),"end")%>']">
<!--Course has already taken place work -->
<B>This course has already taken place.</B>
</xsl:when>
//Another xsl:when could go here
<xsl:otherwise>
<A style="color: olive" href="
title="Click here to register for this course.">
<xsl:attribute name="onclick">
javascript:window.open('register.asp?FullCourseURL=<xsl:value-of
select="d:propstat/d:prop/href" />');
window.event.returnValue=false;</xsl:attribute>
Register for this course
</A>
</xsl:otherwise>
</xsl:choose>
XSL Attribute
Notice the use of the XSL Attribute element in the previous code example. This element allows you to put an attribute on an HTML element inside your XSL template. You should do this if, as part of your HTML element, you want to evaluate another XSL element as the HTML element's value. In the previous example, the onclick attribute is added to the hyperlink (A) element in the HTML. The href to the item is added as the value for the onclick attribute by using the XSL Value-of element.
XSL For-Each
The XSL For-Each element is similar to the Visual Basic For Each…Next loop. The For-Each element allows you to apply a template to an element. The best example of how you can use this element can be found in the first For-Each element that appears in the previous section's code. This For-Each element uses a Select clause and pattern matching to select only the response nodes in the XML. Furthermore, this example uses the Order-by criteria to support sorting the data by a specific node in the XML. The code example from the previous section follows:
<xsl:for-each select="d:multistatus/d:response"
order-by="<%=Request.QueryString("SortBy")%>">
XSL Script and XSL Eval
Note | The <xsl:script> namespace is a Microsoft extension to XSLT. If you are using later versions of the Microsoft XML parser, use the <msxml:script> namespace rather than the <xsl:script> namespace, as in <msxsl:script language="javascript" implements-prefix="user"> </msxsl:script>. |
You'll probably want to use the XSL Script and Eval elements together in your template. The Script element allows you to specify a global script that the rest of your XSL template can call. You can pass the Script element a Language parameter that specifies the scripting language, such as JavaScript, for your script code.The Eval element evaluates a script expression and generates a text string. You'll usually need to call a script you defined by using the Script element in your Eval element and have that script return a text value. You can, however, place inline script in the Eval element as well. The following code gets the date of a given training course object and correctly formats it using the Script and Eval elements:
<xsl:script>
function getMyDate(objThis, szDateFormatString, szTimeFormatSTring)
{
var m_objDate = new Date();
var m_x = 0;
var gszDateString = ";
var szDate = objThis.text;
var szSubStr = szDate.substring(5,7);
if(szSubStr.charAt(0) == "0")
{
szSubStr = szSubStr.charAt(1);
}
m_objDate.setUTCFullYear(szDate.substring(0,4)); //Set Year
m_objDate.setUTCMonth(Number(szSubStr)-1); //Set Month
m_objDate.setUTCDate(szDate.substring(8,10)); //Set Date
m_objDate.setUTCHours(szDate.substring(11,13)); //Set Hours
m_objDate.setUTCMinutes(szDate.substring(14,16)); //Set Minutes
m_objDate.setUTCSeconds(szDate.substring(17,19)); //Set Seconds
var iNumHours = m_objDate.getHours();
var szFormattedTime = formatTime(m_objDate.getVarDate(),
szTimeFormatSTring);
var szFormattedDate = formatDate(m_objDate.getVarDate(),
szDateFormatString);
gszDateString = szFormattedDate.substring(0,szFormattedDate.length-1)
+ " " + szFormattedTime;
return (gszDateString);
}
</xsl:script>
. . .
<!--convert to the correct time zone -->
<TR><TD><B>Start Time:</B></TD><TD>
<xsl:for-each select="d:propstat/d:prop/starttime">
<xsl:eval>getMyDate(this,"MM-dd-yyyy","h:mm tt")
</xsl:eval></xsl:for-each>
</TD></TR>
<TR><TD><B>End Time:</B></TD><TD>
<xsl:for-each select="d:propstat/d:prop/endtime">
<xsl:eval>getMyDate(this,"MM-dd-yyyy","h:mm tt")
</xsl:eval></xsl:for-each>
</TD></TR>
. . .
I've barely begun to scratch the surface of XSL, but this overview should help you get started in transforming your XML using XSL. XSL is still evolving and has not been formally defined in an RFC. The best resource I have found on XSL is the MSDN library at [ http://msdn.microsoft.com/ ]. Also check out the Web Workshop section in the Platform SDK. Not only does it include lots of documentation on XML, it includes a wealth of information on XSL. Last but not least, you might want to check out the Web site of the World Wide Web Consortium (W3C).