alphabetical ordering into groups?

P

prins.henrik

I have the following xml data and would like to format it the following
way with xslt:

1. I need to sort them after there last name (see my example below)
2. I only want to display the main header character (fx. M) if there
are any last names that starts with fx. M
3. I allso need to color every second line under each character
Group..but this is proppery easy so focus on the thing abowe :)

XML data:
------------------
<root>
<Worker>
<Firstname>Andrew</Firstname>
<Lastname>Pitt</Lastname>
</Worker>
<Worker>
<Firstname>Peter</Firstname>
<Lastname>Michael</Lastname>
</Worker>
<Worker>
<Firstname>John</Firstname>
<Lastname>Allisi</Lastname>
</Worker>
<Worker>
<Firstname>Michael</Firstname>
<Lastname>More</Lastname>
</Worker>
</root>

-----------------


The output should look something like this:
---------------

A - M - P


A
John Allisi

M
Michael More
Peter Michael

P
Andrew Pitt

----------------
 
P

Peter Flynn

I have the following xml data and would like to format it the following
way with xslt:

1. I need to sort them after there last name (see my example below)
xsl-sort

2. I only want to display the main header character (fx. M) if there
are any last names that starts with fx. M

XSL FAQ: http://www.dpawson.co.uk/xsl/xslfaq.html, look under
"Muenchian Grouping"
3. I allso need to color every second line under each character
Group..but this is proppery easy so focus on the thing abowe :)

http://www.dpawson.co.uk/xsl/sect2/N7450.html#d9829e82 explains this
for tables. It shouldn't be hard to adapt for your sections.

///Peter
 
M

Mokka

Thanks, but i still have some problems......i am very new to XSLT.

I am trying to use the folloing xslt, but the output isnt right.
"Kalene Bilco" and "Katrine Bower" should both be under "header B".
-------------------------------
Header :A
John Alias
Header :B
Katrine Bower
Header :B
Kalene Bilco
Header :C
Conny Carlzon

------------------------------

My xslt:
--------------------------------

<xsl:key name="contacts-by-surname" match="Worker" use="Lastname" />
<xsl:template match="root">
<xsl:for-each select="Worker[count(. | key('contacts-by-surname',
Lastname)[1]) = 1]">
<xsl:sort select="Lastname" />
<strong>Header :<xsl:value-of select="substring(Lastname, 1,1 )"
/></strong><br />
<xsl:for-each select="key('contacts-by-surname', Lastname)">
<xsl:sort select="Firstname" />
<xsl:value-of select="Firstname" /> <xsl:value-of
select="Lastname" /><br />
</xsl:for-each>
</xsl:for-each>
</xsl:template>
------------------------
 
M

Mokka

Me again :)

im allmost there but my output looks like this now......The problem is
that the B section i in there x2....i need help to remove one of the
sections so i dont have any duplicates.
--------------
A
Jay Arla
B
Katrine Bower
Kane Bingo
B
Katrine Bower
Kane Bingo
C
Kanye Carlson

---------------


XSLT
---------------


<xsl:key name="contacts-by-surname" match="Worker"
use="substring(Lastname, 1, 1)" />
<xsl:template match="root">
<xsl:for-each select="Worker[not(surname =
preceding-sibling::Worker/Lastname)]">
<xsl:sort select="Lastname" />
<strong><xsl:value-of select="substring(Lastname, 1, 1)"
/></strong><br />
<xsl:for-each select="key('contacts-by-surname', substring(Lastname,
1, 1))">
<xsl:sort select="Lastname" />
<div>
<xsl:if test="position() mod 2 != 0">
<xsl:attribute
name="style">background-color:#cccccc;width:200px;</xsl:attribute>
</xsl:if>
<xsl:value-of select="Firstname" /> <xsl:value-of
select="Lastname" /><br />
</div>
</xsl:for-each>
</xsl:for-each>

----------'
 
P

Peter Flynn

Mokka said:
Me again :)

im allmost there but my output looks like this now......The problem is
that the B section i in there x2....i need help to remove one of the
sections so i dont have any duplicates.

You were close the first time. I think this is what you want:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="html"/>
<xsl:strip-space elements="*"/>

<xsl:key name="contacts-by-surname" match="Worker"
use="substring(Lastname,1,1)"/>

<xsl:template match="root">
<p>
<xsl:for-each
select="Worker[count(.|
key('contacts-by-surname',substring(Lastname,1,1))[1])=1]">
<xsl:sort select="Lastname"/>
<a>
<xsl:attribute name="href">
<xsl:text>#</xsl:text>
<xsl:value-of select="substring(Lastname,1,1)"/>
</xsl:attribute>
<xsl:value-of select="substring(Lastname,1,1)"/>
</a>
<xsl:if test="position()!=last()">
<xsl:text> - </xsl:text>
</xsl:if>
</xsl:for-each>
</p>
<xsl:for-each
select="Worker[count(.|
key('contacts-by-surname',substring(Lastname,1,1))[1])=1]">
<xsl:sort select="Lastname"/>
<p>
<b>
<a>
<xsl:attribute name="name">
<xsl:value-of select="substring(Lastname,1,1)"/>
</xsl:attribute>
<xsl:value-of select="substring(Lastname,1,1)"/>
</a>
</b>
<xsl:for-each
select="key('contacts-by-surname',substring(Lastname,1,1))">
<xsl:sort select="Lastname"/>
<xsl:sort select="Firstname"/>
<br/>
<xsl:value-of select="Firstname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="Lastname"/>
</xsl:for-each>
</p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top