-1

Here is a sample XML

<?xml version='1.0' encoding='UTF-8'?>
<message>
<body>
<asset>

    <asset-id>test_01</asset-id>
    <asset-name>report_test</asset-name>
    <asset-version>1</asset-version>
    <entity>
    <id>project_test</id>
        <entity-record>
        <id>864d6141-712b-11e7-b775-f9304f8b3051#-</id>
            <field>
            <id>name</id>
            <value>Transport</value>
            </field>
            <field>
            <id>task</id>
            <value>Person1</value>
            </field>
        </entity-record>
        <entity-record>
        <id>864d6141-812b-11e7-b775-f9304f8b3951#-</id>
            <field>
            <id>name</id>
            <value>Transport</value>
            </field>
            <field>
            <id>task</id>
            <value>Person2</value>
            </field>
        </entity-record>
        <entity-record>
        <id>864d6141-712b-11e7-b775-f9314f8b3951#-</id>
            <field>
            <id>name</id>
            <value>Food </value>
            </field>
            <field>
            <id>task</id>
            <value>Person3</value>
            </field>
        </entity-record>
        <entity-record>
        <id>864e6141-712b-11e7-b775-f9304f8b3951#-</id>
            <field>
            <id>name</id>
            <value>Food</value>
            </field>
            <field>
            <id>task</id>
            <value>Person4</value>
            </field>
        </entity-record>
        <entity-record>
            <field>
            <id>name</id>
            <value>Food</value>
            </field>
            <field>
            <id>task</id>
            <value>Person5</value>
            </field>
        </entity-record>
        <entity-record>
            <field>
            <id>name</id>
            <value>Transport</value>
            </field>
            <field>
            <id>task</id>
            <value>Person3</value>
            </field>
        </entity-record>
    </entity>
</asset>
</body>
</message>

And here is one poor attempt i tried using the Muenchian method for first two cells.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
<xsl:key name="fieldbyid" match="field" use="value" />
<xsl:template match="message">
<html>
<body>
<table border="1">
    <tr>
        <td>Project</td>
        <td>Task</td>
    </tr>
    <tr>
         <xsl:for-each select="body/asset/entity[id[text()='project_test']]/entity-record">
        <xsl:sort select="field[id[text()='name']]/value"/>
        <xsl:for-each select="field[count(./value|key('fieldbyid','value')[1])=1]"> 
         <tr>                
             <td rowspan="{count(key('fieldbyid','value'))}">
             <xsl:value-of select="field[id[text()='name']]/value"/>
             </td>
            <td><xsl:value-of select="field[id[text()='task']]/value"/></td>
        </tr>
        </xsl:for-each>
        </xsl:for-each>
    </tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Here is the output i get

<html>
       <body>
          <table border="1">
             <tr>
                <td>Project</td>
                <td>Task</td>
             </tr>
             <tr>
                <tr>
                   <td>Food</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person4</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Food</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person5</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Food </td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person3</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Transport</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person1</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Transport</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person2</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Transport</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
                <tr>
                   <td>Person3</td>
                   <td rowspan="0"></td>
                   <td></td>
                </tr>
             </tr>
          </table>
       </body>
    </html>

This is how i want my output to look, enter image description here enter image description here On removing the second for loop, that is keeping the code out of for loop, <xsl:for-each select="field[count(./value|key('fieldbyid','value')[1])=1]"> it seems like it atleast displays the values. What does this statement mean exactly? Also on value for "." shows both id and value which is why i used ./value in this case. Any help would be great! Thanks

Ragini
  • 29
  • 1
  • 4

1 Answers1

0

If you're indeed using an XSLT 2.0 processor, then try this as your starting point:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>

<xsl:template match="/message">
    <html>
        <body>
            <table border="1">
                <tr>
                    <th>Project</th>
                    <th>Task</th>
                </tr>
                <!-- group by name -->
                <xsl:for-each-group select="body/asset/entity/entity-record" group-by="field[id='name']/value">
                    <xsl:variable name="tasks">
                        <!-- group by task -->
                        <xsl:for-each-group select="current-group()" group-by="field[id='task']/value">
                            <tr>
                                <td>
                                    <xsl:value-of select="current-grouping-key()" />
                                </td>
                            </tr>   
                        </xsl:for-each-group>       
                    </xsl:variable>
                    <tr>
                        <td rowspan="{1 + count($tasks/tr)}">
                            <xsl:value-of select="current-grouping-key()" />
                        </td>
                    </tr>
                    <xsl:copy-of select="$tasks"/>
                </xsl:for-each-group>               
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

The output should look something like this :

enter image description here

This is after adjusting your input example to remove the extra space in <value>Food </value>.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51