Subject: [xsl] Answers to review questions in "Beginning XSLT": Chapter 10 From: "Lars Huttar" <lars_huttar@xxxxxxx> Date: Thu, 20 Mar 2003 21:54:13 -0600 |
Dear all, This will probably be the last chapter I'll do for a while. My purpose in going through "Beginning XSLT" has been to get a solid enough foundation in XSLT to use it for a project (for my MA thesis). I feel confident enough now to turn attention to the project, coming back to this book and others when needed. Many thanks to everyone on this list who has helped me get up to speed by answering my questions and others'! Thanks too to Kay and Tennison for their excellent books. And Jeni for individualized tutoring. Lars 1. What is the advantage of using ID attributes and the id() function over using predicates to search for elements? Answer: efficiency. 2. What format can the values of ID attributes take? Answer: They have to be XML Names, which means they must start with a letter, underscore, or colon, and contain only "alphanumeric" characters (which include '.' | '-' | '_' | ':' plus other similar punctuation in Unicode). In particular, no spaces are allowed. 3. Create an ATTLIST declaration to make the required custNo attribute of the <Customer> element an ID attribute. Answer: <!DOCTYPE foo [ <!ELEMENT Customer (ANY)> <!ATTLIST Customer custNo ID #REQUIRED> ]> 4. What three types of arguments can the id() function take and what does it return from each? Answer: id() can take a string containing a single ID, in which case it returns the element that has that ID. id() can take a string containing multiple IDs separated by space, in which case it returns a node set consisting of the elements that have those IDs. id() can take a node set, in which case id() returns a node set consisting of the elements whose IDs are the values of nodes in the argument node set. 5. What advantages are there for using keys rather than IDs? Answer: a. You're not dependent on an external or internal DTD to declare attributes as type ID. b. You can key on other values than just attributes, sometimes complex ones. c. Key values can have unrestricted format. d. You can have different key spaces for different kinds of elements, making indices more efficient and making it easier to come up with unique key values within a key space. e. Multiple elements can have the same key value if desired, and each element can have several key values. 6. In what situations might you us the keys defined as follows? Answers: <xsl:key name="films" match="Film" use="@id" /> When you have a number of <Film> elements that you want to access by their @id attribute. <xsl:key name="filmsByCharacters" match="Film" use="CastList/CastMember/Character/Name" /> When you want to group the films by the name of a character in the cast. Maybe you want to list the characters one by one, and under each character, list the films they're in. This is a many-to-many key. <xsl:key name="filmsByYear" match="Film" use="Year" /> When you want to group the films by the year in which they were made (the value of the <Year> element child of <Film>). Perhaps the purpose is to list all films made in 1970, then all those made in 1971, etc. This is a one-to-many key. <xsl:key name="filmsByDirector" match="Film" use="Director/Name" /> When you want to group the films by the name of the director (the value of the <Name> element child of the <Director> element child[ren] of <Film>). This could be a many-to-many key, in the case of multiple directors per film; or just one-to-many. <xsl:key name="filmsByYearAndDirector" match="Film" use="concat(Year, ':', Director/Name)" /> When you want to group the films by both year and director. In conjunction with one of the previous two keys, you could use this to list films first by year, then within that by director; or vice versa. 7. Construct a stylesheet that groups <Film> elements by their <Year> children and by their rating attributes. Answer: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:key name="FilmsByYear" match="Film" use="Year" /> <xsl:key name="FilmsByRating" match="Film" use="@rating" /> <xsl:template match="/"> <html> <head> <style> .filmname { font-style: italic } </style> </head> <body> <h1>Films, by Year</h1> <xsl:apply-templates select="Films/Film[generate-id() = generate-id(key('FilmsByYear', Year)[1])]" mode="YearList"> <xsl:sort select="Year" data-type="number"/> </xsl:apply-templates> <h1>Films, by Rating</h1> <xsl:apply-templates select="Films/Film[generate-id() = generate-id(key('FilmsByRating', @rating)[1])]" mode="RatingList"> <xsl:sort select="@rating" order="descending" data-type="number" /> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="Film" mode="YearList"> <h2><xsl:value-of select="Year" /></h2> <xsl:apply-templates select="key('FilmsByYear', Year)" mode="Detail"> <xsl:sort select="@rating" data-type="number" order="descending" /> </xsl:apply-templates> </xsl:template> <xsl:template match="Film" mode="RatingList"> <h2><xsl:value-of select="@rating" /> stars</h2> <xsl:apply-templates select="key('FilmsByRating', @rating)" mode="Detail"> <xsl:sort select="Year" data-type="number" /> </xsl:apply-templates> </xsl:template> <xsl:template match="Film" mode="Detail"> <p> <span class="filmname"><xsl:value-of select="Name" /></span>, <xsl:value-of select="Year" />. Stars: <xsl:value-of select="@rating" /> </p> </xsl:template> </xsl:stylesheet> Test data: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Review7.xsl"?> <!-- These ratings and years mostly have nothing to do with reality. It's just test data. --> <Films> <Film rating="3"> <Name>Revenge of the Killer Tomatoes</Name> <Year>1962</Year> </Film> <Film rating="5"> <Name>Crouching Tiger, Hidden Dragon</Name> <Year>2001</Year> </Film> <Film rating="4"> <Name>Chicken Run</Name> <Year>2000</Year> </Film> <Film rating="3"> <Name>Gone with the Cloud</Name> <Year>1999</Year> </Film> <Film rating="4"> <Name>Woman Reporter</Name> <Year>1999</Year> </Film> <Film rating="5"> <Name>Long Way from Home</Name> <Year>2000</Year> </Film> <Film rating="3"> <Name>The Unforgettable Character</Name> <Year>1999</Year> </Film> <Film rating="3"> <Name>Evergreen Tree</Name> <Year>2001</Year> </Film> <Film rating="4"> <Name>Orchid in the Rain</Name> <Year>2001</Year> </Film> <Film rating="5"> <Name>Hour of the Wolf</Name> <Year>2000</Year> </Film> <Film rating="4"> <Name>Seven Foxes</Name> <Year>1999</Year> </Film> </Films> Lars Huttar MA student at Graduate Institute for Applied Linguistics, www.gial.edu web page: http://www.cs.oberlin.edu/~huttar/home.html Blind unbelief is sure to err, and scan his work in vain; God is his own interpreter, and he will make it plain. William Cowper, 1774 XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] display special html char, Mike Brown | Thread | Re: [xsl] Answers to review questio, Jeni Tennison |
Re: [xsl] where does one stick the , Mike Brown | Date | Re: [xsl] display special html char, Mike Brown |
Month |