Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 12th, 2007, 02:24 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum of sub-matrix of a matrix Gromlok Java Basics 0 March 4th, 2007 03:45 PM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM
matrix report rhbandagi Classic ASP Databases 0 March 24th, 2006 04:06 AM
Dynamic Matrix Spaceskapie Classic ASP Professional 1 October 15th, 2004 08:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.