XSLT - MATRIX
Hi,
I have one xml file that contained tree tables. Please see the code for the .xml and .xsl files below:
Table_A: RowID, ID, ColID, Data
Table_B: ID, Index_B
Table_C: Name ID Index_C
I would like to write a stylesheet (.xsl) that display the result in a matrix with these conditions below:
1. Display the Name in Table_C
2. Check the Table_C/ID = Table_A/RowID
3. If yes, get/check the Table_A/ColID = Table_B/ID
4. If yes, get the index() in Table_B
5. Display the value of the Data in Table_A base on the position of the index() in Table_B.
Note: I could have more that one Table_A/RowID with the same value but the Table_A/ColID must be difference. Is there a way to display the matrix without hard code the value for each RowID, ColID, and ID?
<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="matrix.xsl"?>
<Matrix>
<Table_A>
<RowID>1001</RowID>
<ID>0</ID>
<ColID>5001</ColID>
<Data>YY</Data>
</Table_A>
<Table_A>
<RowID>1001</RowID>
<ID>0</ID>
<ColID>5005</ColID>
<Data>YY</Data>
</Table_A>
<Table_A>
<RowID>1001</RowID>
<ID>0</ID>
<ColID>5003</ColID>
<Data>YY</Data>
</Table_A>
<Table_A>
<RowID>1001</RowID>
<ID>0</ID>
<ColID>5006</ColID>
<Data>YY</Data>
</Table_A>
<Table_A>
<RowID>1002</RowID>
<ID>0</ID>
<ColID>5001</ColID>
<Data>YY</Data>
</Table_A>
<Table_A>
<RowID>1003</RowID>
<ID>0</ID>
<ColID>5002</ColID>
<Data>Y</Data>
</Table_A>
<Table_A>
<RowID>1004</RowID>
<ID>0</ID>
<ColID>5003</ColID>
<Data>Y</Data>
</Table_A>
<Table_A>
<RowID>1005</RowID>
<ID>0</ID>
<ColID>5004</ColID>
<Data>YYY</Data>
</Table_A>
<Table_A>
<RowID>1006</RowID>
<ID>0</ID>
<ColID>5005</ColID>
<Data>YYY</Data>
</Table_A>
<Table_A>
<RowID>1007</RowID>
<ID>0</ID>
<ColID>5006</ColID>
<Data>YYY</Data>
</Table_A>
<Table_B>
<ID>5001</ID>
<Index_B>0</Index_B>
</Table_B>
<Table_B>
<ID>5002</ID>
<Index_B>1</Index_B>
</Table_B>
<Table_B>
<ID>5003</ID>
<Index_B>2</Index_B>
</Table_B>
<Table_B>
<ID>5004</ID>
<Index_B>3</Index_B>
</Table_B>
<Table_B>
<ID>5005</ID>
<Index_B>4</Index_B>
</Table_B>
<Table_B>
<ID>5006</ID>
<Index_B>5</Index_B>
</Table_B>
<Table_B>
<ID>5006</ID>
<Index_B>6</Index_B>
</Table_B>
<Table_C>
<Name>AAA</Name>
<ID>1001</ID>
<Index_C>0</Index_C>
</Table_C>
<Table_C>
<Name>BBB</Name>
<ID>1002</ID>
<Index_C>1</Index_C>
</Table_C>
<Table_C>
<Name>CCC</Name>
<ID>1003</ID>
<Index_C>2</Index_C>
</Table_C>
<Table_C>
<Name>DDD</Name>
<ID>1004</ID>
<Index_C>3</Index_C>
</Table_C>
<Table_C>
<Name>FFF</Name>
<ID>1005</ID>
<Index_C>4</Index_C>
</Table_C>
<Table_C>
<Name>GGG</Name>
<ID>1006</ID>
<Index_C>5</Index_C>
</Table_C>
<Table_C>
<Name>HHH</Name>
<ID>1007</ID>
<Index_C>6</Index_C>
</Table_C>
</Matrix>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<body>
<h2>
<center>Matrix</center>
</h2>
<table border="1">
<tbody>
<tr>
<th colspan="4">TABLE_A</th>
</tr>
<tr>
<th>RowID</th>
<th>ID</th>
<th>ColID</th>
<th>Data</th>
</tr>
<xsl:for-each select="//Table_A">
<tr>
<td><xsl:value-of select="RowID"/></td>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="ColID"/></td>
<td><xsl:value-of select="Data"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<table border="1">
<tbody>
<tr>
<th colspan="2">TABLE_B</th>
</tr>
<tr>
<th>ID</th>
<th>Index_B</th>
</tr>
<xsl:for-each select="//Table_B">
<tr>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="Index_B"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<table border="1">
<tbody>
<tr>
<th colspan="3">TABLE_C</th>
</tr>
<tr>
<th>Name</th>
<th>ID</th>
<th>Index_C</th>
</tr>
<xsl:for-each select="//Table_C">
<tr>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="ID"/></td>
<td><xsl:value-of select="Index_C"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<table border="1">
<tbody>
<tr>
<th colspan="8">Final Table</th>
</tr>
<tr>
<th>Name</th>
<xsl:for-each select="//Table_B">
<th><xsl:value-of select="Index_B"/></th>
</xsl:for-each>
</tr>
<xsl:for-each select="//Table_C">
<xsl:choose>
<xsl:when test="Index_C ='0' ">
<tr>
<td><xsl:value-of select="Name"/></td>
<xsl:for-each select="//Table_B">
<xsl:choose>
<xsl:when test="Index_B='0'">
<td>
<xsl:for-each select="//Table_A">
<xsl:choose>
<xsl:when test="RowID = //Table_C/ID and ColID = //Table_B/ID">
<xsl:value-of select="Data "/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</td>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</tr>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Below is a sample of the matrix output.
Name 0 1 2 3 4 5 6
AAA YY YY YY YY
BBB YY
CCC Y
DDD Y
EEE YYY
FFF YYY
GGG YYY
Thanks in advance.
-John
|