I’m applying an XSLT stylesheet to an XML chat log and want to only display the chatter’s name the first time it appears in consecutive groups of messages, so that multiple lines are “grouped” by chatter in each message group. An example illustrates this better.
I want to go from this:
<Cthon98> hey, if you type in your pw, it will show as stars
<Cthon98> ********* see!
<AzureDiamond> hunter2
<AzureDiamond> doesnt look like stars to me
To this:
<Cthon98> hey, if you type in your pw, it will show as stars
********* see!
<AzureDiamond> hunter2
doesnt look like stars to me
My XSL (which is iterated once per chat line) is this:
<xsl:template match="User">
<!-- add a comma before all but the first user -->
<xsl:if test="position() != 1">, </xsl:if>
<!-- Pseudocode:
1. Set variable to name of current chatter
2. Set variable to name of previous line's chatter
3. If current chatter == previous chatter, don't display name
4. If current chatter != previous chatter, display name
-->
<!-- This displays the name -->
<xsl:value-of select="@FriendlyName"/>
</xsl:template>
Can someone help me convert that pseudocode? Thanks so much!
Edit: the input XML is essentially a repetition of the following to/from message structure:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="20">
<Message>
<From><User FriendlyName="chatter1"/></From>
<To><User FriendlyName="chatter2"/></To>
<Text>hey</Text>
</Message>
<Message>
<From><User FriendlyName="chatter2"/></From>
<To><User FriendlyName="chatter1"/></To>
<Text>hey!</Text>
</Message>
</Log>
The easiest way to prevent the name from showing for consecutive messages from the same user is to check the
preceding-siblingaxis. The following stylesheet:Applied to the following input:
Produces the following output:
Edit: I would probably handle each case in its own template, like this: