Conditionals - Exercise

Contact Us or call 1-877-932-8228
Conditionals - Exercise

Conditionals

Duration: 20 to 30 minutes.

In this exercise, you will practice using xsl:if and xsl:choose.

  1. Open FlowControl/Exercises/Conditions.xsl.
  2. Modify the first xsl:for-each element, so the Title element only shows up on the output if the Name element in the source has a child Title attribute.
  3. Modify the second xsl:for-each element, so that
    • All elements containing the text "Webucator" output a Match element with a Text attribute with the value of "W". The Match element should contain the name and text of the matched element. Use the String Functions table as a reference.
    • All elements containing the text "Lockwood & Lockwood", output a Match element with a Text attribute with the value of "L&L". Again, the Match element should contain the name and text of the matched element.
    • If neither string is matched, output a NoMatch element containing the name and text of the element with no match.
  4. To test your solution, transform FlowControl/Exercises/Conditions.xml against FlowControl/Exercises/Conditions.xsl. The intended output is shown below.

Code Sample:

FlowControl/Exercises/ConditionsOutput.xml
<?xml version="1.0" encoding="UTF-8"?>
<Names>
	<Name>
		<Title>Mr.</Title>
		<FName>Joshua</FName>
		<LName>Lockwood</LName>
	</Name>
	<Name>
		<FName>Bill </FName>
		<LName>Smith</LName>
	</Name>
	<Name>
		<FName>Bill </FName>
		<LName>Smith</LName>
	</Name>
</Names>
<Matches>
	<NoMatch>SendDate: November 29, 2005</NoMatch>
	<NoMatch>FirstName: Joshua</NoMatch>
	<NoMatch>LastName: Lockwood</NoMatch>
	<Match Text="L&amp;L">Company: Lockwood &amp; Lockwood</Match>
	<NoMatch>Street: 291 Broadway Ave.</NoMatch>
	<NoMatch>City: New York</NoMatch>
	<NoMatch>State: NY</NoMatch>
	<NoMatch>Zip: 10007</NoMatch>
	<NoMatch>Country: United States</NoMatch>
	<NoMatch>Heading: Along with this letter, I have enclosed the following 
		items:</NoMatch>
	<Match Text="W">ListItem: two original, execution copies of the Webucator 
		Master Services Agreement</Match>
	<Match Text="W">ListItem: two original, execution copies of the Webucator 
		Premier Support for Developers Services Description between Lockwood 
		&amp; Lockwood and Webucator, Inc.</Match>
	<NoMatch>Para: Please sign and return all four original, execution copies
		to me at your earliest convenience.  Upon receipt of the executed copies, 
		we will immediately return a fully executed, original copy of both 
		agreements to you.</NoMatch>
	<NoMatch>Para: Please send all four original, execution copies to my 
		attention as follows:
			</NoMatch>
	<NoMatch>FirstName: Bill </NoMatch>
	<NoMatch>LastName: Smith</NoMatch>
	<Match Text="W">Company: Webucator, Inc.</Match>
	<NoMatch>Street: 4933 Jamesville Rd.</NoMatch>
	<NoMatch>City: Jamesville</NoMatch>
	<NoMatch>State: NY</NoMatch>
	<NoMatch>Zip: 13078</NoMatch>
	<NoMatch>Country: USA</NoMatch>
	<NoMatch>Para: If you have any questions, 
		feel free to call me at </NoMatch>
	<NoMatch>Phone: 800-555-1000 x123</NoMatch>
	<NoMatch>Email: bsmith@webucator.com</NoMatch>
	<NoMatch>FirstName: Bill </NoMatch>
	<NoMatch>LastName: Smith</NoMatch>
	<NoMatch>JobTitle: VP, Operations</NoMatch>
</Matches>

Solution:

FlowControl/Solutions/Conditions.xsl
<?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" indent="yes"/>
	<xsl:template match="/">
		<Names>
		<xsl:for-each select="//Name">
				<Name>
					<xsl:if test="@Title">
						<Title><xsl:value-of select="@Title"/></Title>
					</xsl:if>
					<FName><xsl:value-of select="FirstName"/></FName>
					<LName><xsl:value-of select="LastName"/></LName>
				</Name>
		</xsl:for-each>
		</Names>
		<Matches>
			<xsl:for-each select="//*[text()]">
				<xsl:choose>
					<xsl:when test="contains(text(),'Webucator')">
						<Match Text="W"><xsl:value-of select="name()"/>: 
						<xsl:value-of select="text()"/></Match>
					</xsl:when>
					<xsl:when test="contains(text(),'Lockwood &amp; Lockwood')">
						<Match Text="L&amp;L"><xsl:value-of select="name()"/>: 
						<xsl:value-of select="text()"/></Match>
					</xsl:when>
					<xsl:otherwise>
						<NoMatch><xsl:value-of select="name()"/>: 
						<xsl:value-of select="text()"/></NoMatch>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</Matches>
	</xsl:template>
</xsl:stylesheet>