|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
lets say I have xml like this: Code:
<books> <book> <author>Stephen King</auhtor> <name>something</name> </book> <book> <author>Stephen King</auhtor> <name>something2</name> </book> <book> <author>Agatha Christie</auhtor> <name>something3</name> </book> </books> If I loop all books with for-each, is it possible to generate somekind of array of authors and number of the books, like in this case Stephen King, 2 Agatha Chistie 1. In the software I'm doing I need to get output where books are listed according to the author and since the actual data comes from database I don't know the number of different authors and how many books each of them have. ..add here all the "I'm a newbie in XSL/XML.." stuff And thanks in advance if somebody can help me out with this! |
|
#2
|
|||
|
|||
|
Hmm.. maybe I wasn't clear enough..
Well if you have xml (from database) like in my previous post, how would you generate a listing with xsl that would be shown like this: Author, Number of books Stephen King, 2 Agatha Christie, 1 ..and so on.. Exept that you don't know the names of the authors or how many different authors there are (names shown in this example are just for clearing this out), it has to be figured out with xsl. I don't know how to do that.. There must be somebody who knows how to do this ![]() |
|
#3
|
|||
|
|||
|
Yeah!
If you want it how I think you want it, then try this stylesheet below! <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <html> <head> <title></title> </head> <body> <table border="1"> <tbody> <tr align="left"> <th>Author</th><th>No. of Books</th> </tr> <xsl:for-each select="//book[not(./author = preceding::book/author)]"> <xsl:variable name="auth"><xsl:value-of select="author" /></xsl:variable> <tr> <td><xsl:value-of select="author" /></td> <td><xsl:value-of select="count(//author[text() = $auth]) " /></td> </tr> </xsl:for-each> </tbody> </table> </body> </html> </xsl:template> </xsl:stylesheet> I think it does what you asked. Enjoy! |
|
#4
|
|||
|
|||
|
That's perfect!! Thank you, just what I was looking for.
Quote:
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Dynamic array in XSL? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|