Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Smartest way to sort data with XSLT and display it

From: "Joris Gillis" <roac@-------.-->
To: NULL
Date: 8/2/2005 11:35:00 AM
Hi,

Tempore 11:18:00, die Tuesday 02 August 2005 AD, hinc in foro {comp.text.xml} scripsit Omega375 <safaridonna@g...>:

> What I now want to do (with XSLT) is to display all the books by the
> author, eg;
>
> This can easly be done by using xpath "books/book[@author='JK
> Rowling']".
Yes, but it's more CPU-friendly to use keys.

> This is where my problem is;
> I don't knwo which authors that do exist in the xml-file.

In some way, you need to obtain a list containing all possible author names without duplicates. This can be achieved with grouping. That's a built in job in XSLT 2.0, but in XSLT 1.0, you need to build a grouping algorithm yourself.
Consider this example (using the popular Muenchian grouping technique):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:key name="bookByAuthor" match="book" use="@author"/>
<xsl:variable name="allAuthors" select="//book[generate-id()=generate-id(key('bookByAuthor',@author)[1])]/@author"/>

<xsl:template match="books">
	<xsl:for-each select="$allAuthors">
		<xsl:value-of select="."/>
		<xsl:text>&#10;</xsl:text>
		<xsl:apply-templates select="key('bookByAuthor',.)"/>
	</xsl:for-each>
</xsl:template>

<xsl:template match="book">
	<xsl:text>	* </xsl:text>
	<xsl:apply-templates/>
	<xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="text()">
	<xsl:value-of select="normalize-space(.)"/>
</xsl:template>

</xsl:stylesheet>


regards,
-- 
Joris Gillis (http://users.telenet.be/root-jg/me.html)
«Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω»


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent