[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: cocoon, sql, & xslt
Kari, >What I don't know how to do is display the customer name once with a list >of projects. >From your XSLT, I'm guessing that your input XML looks like: <PROJECTS> <PROROW> <id>1</id><name>Customer 1</name><project_name>Project 1</project_name> </PROROW> <PROROW> <id>2</id><name>Customer 1</name><project_name>Project 2</project_name> </PROROW> <PROROW> <id>3</id><name>Customer 2</name><project_name>Project 1</project_name> </PROROW> </PROJECTS> You are trying to group the rows according to the customer name. One of the ways of dealing with grouping problems like this is to use the Muenchian method. First, define a key to group the rows according to the customer. You define a key using the xsl:key element, which has to go at the top level in the stylesheet (right under xsl:stylesheet). xsl:key takes three attributes: * name - the name of the key, anything you like, but usually the thing you're grouping: 'rows' in this case * match - the nodes that you want to match - in your case, the PROROW elements * use - the key that you want to use to group the nodes expressed as an XPath from the thing you're matching to the key that you want to use to access those nodes - in your case, the value of the 'name' element child of the PROROW So, your xsl:key element should look something like: <xsl:key name="rows" match="PROROW" use="name" /> The key indexes each of the rows according to the customer. You can use the key() function to get the list of rows relating to a customer, so key('rows', 'Customer 1') gives you a list of the PROROW elements with ids 1 and 2, whereas key('rows', 'Customer 2') gives you the PROROW element with id 3. This makes it easy to get to your rows once you know the name of the customer. Next you have to arrange it so that you can get a list of the customers, in which each customer only appears once. You've grouped the rows within the key, and you know each group relates to one particular customer - it's a matter of identifying one row from each group - the first, say. The way you do this is through an XPath expression that identifies all the rows that appear first in their group: PROROW[generate-id(.) = generate-id(key('rows', name)[1])] This says: Find a PROROW element such that A unique ID generated for that element is the same as A unique ID generated for The first node in the node list returned by Indexing the 'rows' key on the value of The 'name' child element of that element If you apply templates only to those PROROW elements, then you know that you are applying templates to one PROROW per customer: <xsl:template match="PROJECTS"> <xsl:apply-templates select="PROROW[generate-id(.) = generate-id(key('rows', name)[1])]" /> </xsl:template> Your PROROW-matching template should now output information about all the rows relating to the customer named in that PROROW. You can access them through the key() function as described earlier, and cycle through them using xsl:for-each (or through calling xsl:apply-templates, if you want). <xsl:template match="PROROW"> <b><xsl:value-of select="name" /></b> <ul> <xsl:for-each select="key('rows', name)"> <li> <a href="projects_results.xml?project={id}"> <xsl:value-of select="project_name" /> </a> </li> </xsl:for-each> </ul> </xsl:template> [Notes: I've changed your HTML elements into lower case to make them XHTML compatible. The links go to the id of the *row* rather than of the project - I don't know if this is what is desired. Tested and works in SAXON.] I hope that helps, Jeni Dr Jeni Tennison Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE Telephone 0115 9061301 ? Fax 0115 9061304 ? Email jeni.tennison@xxxxxxxxxxxxxxxx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|