<!-- input file -->
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="crosstab.xslt" version="1.0"?>
<ActivityList>
<Item>
<Day>1</Day>
<Type>Meal</Type>
<Name>Breakfast</Name>
<Note></Note>
</Item>
<Item>
<Day>1</Day>
<Type>Meal</Type>
<Name>Dinner</Name>
<Note>lots of stuff</Note>
</Item>
<Item>
<Day>1</Day>
<Type>Tour</Type>
<Name>Great wall</Name>
<Note></Note>
</Item>
<Item>
<Day>2</Day>
<Type>Meal</Type>
<Name>Breakfast</Name>
<Note></Note>
</Item>
<Item>
<Day>2</Day>
<Type>Airport</Type>
<Name>Transfer to hotel</Name>
<Note></Note>
</Item>
<Item>
<Day>3</Day>
<Type>Airport</Type>
<Name>Transfer to airport</Name>
<Note></Note>
</Item>
<Item>
<Day>3</Day>
<Type>Meeting</Type>
<Name>Face to face meeting</Name>
<Note></Note>
</Item>
</ActivityList>
<!-- xslt -->
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="distinctDayList" select="/ActivityList/Item[not(Day=preceding-sibling::Item/Day)]/Day"></xsl:variable>
<xsl:variable name ="distinctTypeList" select="/ActivityList/Item[not(Type=preceding-sibling::Item/Type)]/Type"></xsl:variable>
<html>
<header>
<style type="text/css">
table, td, th
{
border:solid 1px black
}
table
{
border-collapse:collapse;
}
</style>
</header>
<body>
<h1>Cross tab demo</h1>
<table>
<tr>
<!--build day row-->
<th>Type</th>
<xsl:for-each select="$distinctDayList">
<xsl:sort select="Day"/>
<th>
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="$distinctTypeList">
<xsl:sort select="Type" data-type="text"/>
<tr>
<td>
<xsl:value-of select="."/>
</td>
<xsl:variable name="currentType" select="."></xsl:variable>
<xsl:for-each select="$distinctDayList" >
<td>
<xsl:for-each select="/ActivityList/Item[Day=current() and Type=$currentType]">
<xsl:value-of select="Name"/>
<xsl:if test="position()!=last()">
,
</xsl:if>
</xsl:for-each>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<!-- output file -->
<html><header><style type="text/css">
table, td, th
{
border:solid 1px black
}
table
{
border-collapse:collapse;
}
</style></header><body>
<h1>Cross tab demo</h1>
<table>
<tr>
<th>Type</th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<tr>
<td>Meal</td>
<td>Breakfast
,
Dinner</td>
<td>Breakfast</td>
<td></td>
</tr>
<tr>
<td>Tour</td>
<td>Great wall</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Airport</td>
<td></td>
<td>Transfer to hotel</td>
<td>Transfer to airport</td>
</tr>
<tr>
<td>Meeting</td>
<td></td>
<td></td>
<td>Face to face meeting</td>
</tr>
</table>
</body>
</html>
Monday, December 22, 2008
XSLT note 5 - Cross tab transformation
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment