Subject: [xsl] Answers to review questions in "Beginning XSLT": Chapter 6 From: "Lars Huttar" <lars_huttar@xxxxxxx> Date: Wed, 12 Mar 2003 20:02:17 -0600 |
Hello, Here are my answers to the review questions in Chapter 6 of Jeni Tennison's "Beginning XSLT" (2002, Wrox). They are offered in hopes they'll be helpful to others using the book, and in hopes of discovering if I've failed to learn something important. 1. What are the four types of values that XPath expressions can evaluate to, and what fifth value type is used in XSLT? Answer: XPath expressions can evaluate to numbers, strings, boolean, and node sets. Result tree fragments are also used in XSLT. 2. What types of values do the following variables hold? <xsl:variable name="price" select="Price" /> <xsl:variable name="keyword">sport</xsl:variable> <xsl:variable name="good" select="@rating > 6" /> <xsl:variable name="date" select="substring-before(Start, 'T')" /> <xsl:variable name="duration" select="substring(Duration, 3, 1) * 60" /> Answer: price holds a node set (the set of all child elements of the context node named "Price"). keyword holds a result tree fragment (RTF) because the value is given in the content of the <xsl:variable> element instead of in the select attribute. good holds a boolean. date holds a string. duration holds a number. 3. What is the difference between <xsl:copy-of> and <xsl:value-of>? Answer: The difference is that the latter gives the string value of its select expression, which in the case of a RTF or node-set would be simply the text in the text nodes; whereas the former copies all (the elements, attributes, and other nodes) of the result of the select expression. 4. Correct the error in the following piece of XSLT: <xsl:choose> <xsl:when test="@rating > 6"> <xsl:variable name="rating" select="'high'" /> </xsl:when> <xsl:when test="@rating < 4"> <xsl:variable name="rating" select="'low'" /> </xsl:when> <xsl:otherwise> <xsl:variable name="rating" select="'medium'" /> </xsl:otherwise> </xsl:choose> <xsl:value-of select="$rating" /> Answer: <!-- Note: the question says "correct the error", but in fact there are two errors. The main one is declaring variables inside elements that you need to reference the variable outside of. The second is an unescaped less-than sign. (I also escape greater- than signs because it helps my XSL editor indent things properly.) --> <xsl:variable name="rating"> <xsl:choose> <xsl:when test="@rating > 6">high</xsl:when> <xsl:when test="@rating < 4">low</xsl:when> <xsl:otherwise>medium</xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:value-of select="$rating" /> We have given rating a different value here than in the original code, namely a result tree fragment rather than a string; but that's OK because the <xsl:value-of> statement converts its value to a string. 5. Create a piece of XSLT that will take a date in the US date format M/D/YYYY and convert it to the standard date format YYYY-MM-DD. The date 9/4/2001 should get converted to 2001-09-04. Answer: <xsl:template match="date"> <xsl:variable name="M" select="substring-before(., '/')" /> <xsl:variable name="D" select="substring-before(substring-after(., '/'), '/')" /> <xsl:variable name="Y" select="substring-after(substring-after(., '/'), '/')" /> <xsl:value-of select="concat($Y, '-', format-number($M, '00'), '-', format-number($D, '00'))" /> <br /> </xsl:template> 6. Given the following decimal formats, create some XSLT that will output a number formatted in English, French, and German style: <xsl:decimal-format name="French" decimal-separator="," grouping-separator=" " /> <xsl:decimal-format name="German" decimal-separator="," grouping-separator="." /> Answer: <xsl:template match="num"> <p> French: <xsl:value-of select="format-number(., '# ##0,00', 'French')"/> <br /> German: <xsl:value-of select="format-number(., '#.##0,00', 'German')"/> <br /> English: <xsl:value-of select="format-number(., '#,##0.00')"/> </p> </xsl:template> 7. What are the differences between a global variable and a stylesheet parameter? Answer: 1) Stylesheet parameters are declared with <xsl:param> instead of <xsl:variable>. 2) You can override the (default) value of a stylesheet parameter by specifying a value on the command line, or in some other way depending on how you invoke the XSL processor. 8. What will the following XSLT stylesheet normally output? <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="author" select="$defaultAuthor" /> <xsl:variable name="defaultAuthor" select="'A.N.Other'" /> <xsl:template match="/"> <xsl:param name="by" /> <xsl:apply-templates select="." mode="author" /> </xsl:template> <xsl:template match="/" mode="author"> <xsl:param name="by" select="$author" /> Written by: <xsl:value-of select="$by" /> </xsl:template> </xsl:stylesheet> Answer: Normally it will output "Written by: A.N.Other", but if the 'author' parameter is given on the command line, its value will replace "A.N.Other". The second template declares the parameter 'by', which is not passed in, so it defaults to $author, which is the global author parameter which defaults to $defaultAuthor. That's it... comments are welcome. Anything I missed? Lars XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] XSL:Copy w/ Processing Op, Michael Kay | Thread | Re: [xsl] Answers to review questio, Mike Brown |
Re: [xsl] XSL:Copy w/ Processing Op, martin | Date | Re: [xsl] XSL:Copy w/ Processing Op, martin |
Month |