Context
This is for a Custom Query Web Part (CQWP) in SharePoint 2007. I am extracting a list of event from a calendar and I want to display a list of current system State for all system, even if they have no current event.
The list is ordered by system name.
XML received
Suppose you have either XML :
<events>
<event>
<system>A</system>
<state>1</state>
</event>
<event>
<system>B</system>
<state>2</state>
</event>
<event>
<system>C</system>
<state>3</state>
</event>
</events>
OR
<events>
<event>
<system>A</system>
<state>1</state>
</event>
<event>
<system>C</system>
<state>2</state>
</event>
</events>
Note : 3 static system (A,B or C). They can have no current event (I didn’t put start/end date as it was unneeded for the question.)
Data output wanted
I want my XSL output to be like :
<table>
<tr>
<th>System</th>
<th>State</th>
</tr>
<tr>
<td>A</td>
<td>1</td>
</tr>
<tr>
<td>B</td>
<td>1</td>
</tr>
<tr>
<td>C</td>
<td>3</td>
</tr>
</table>
That is, I want to have output for all 3 systems, even if they don’t have a current event (hence, not in the XML (Default state is 1)). Oh and yeah, 1 system could have 2+ current event. ( <td>A</td><td>1</td></tr><tr><td>A</td><td>2</td>). A perfect answer would concatenate same system event and only display the highest state, but I can do without.
Current XSL
Here’s the current template I have :
<xsl:template name="system" match="Row[@Style='system']" mode="itemstyle">
<xsl:if test="count(preceding-sibling::*)=0">
<table><tr><th>System</th>
<th>State</th></tr>
</xsl:if>
<tr>
<td><xsl:value-of select="@System"/></td>
<td><xsl:value-of select="@State"/></td>
</tr>
<xsl:if test="count(following-sibling::*)=0">
</table>
</xsl:if>
</xsl:template>
What I thought
I though I could use a variable (or 3?) to contain my list of (static) system
<xsl:variable name="Systems">A,B,C</xsl:variable>
<xsl:variable name="System1">A</xsl:variable>
<xsl:variable name="System2">B</xsl:variable>
<xsl:variable name="System3">C</xsl:variable>
Then, in XSL, check if <xsl:value-of select="@System"/> ever equals this/those variable value.
Questions
Is it doable?
Would you suggest me to proceed otherwise, if so, how?
How can I compare my variable(s) to <xsl:value-of select="@System"/>?
Bonus: How can I concatenate same system event and only display the highest state?
Thanks for both answerer, your example helped me understand XSL. However, since it was for SharePoint, I became aware that I’d have to play within the framework…
The case no current event is handled in the mainStyle.xsl and only display default html.
Items are handled in itemStyle.xsl, one by one. Most of my code was placed there.
First, I created a bunch of variables:
I then build up my solution, starting with a header when I was at my first node
If a System had no event, I’d know it with my variable, and display accordingly
As for the display of the max value only, this was abandoned. There’s some value to know that you have 2 current events impacting a system in the end.
This solution work, but it is sorta dumb, creating lots of variables per items to do test on a group… To optimize, a highly modified mainstyle.xsl or a full blown WebPart would be required.