0

I have some xslt script to parse the xml input and return the html file. So far it works fine. But now I need to parse the xml input to get this complex table with merging headers. What I want the output is below.

|       V1         ||          V2        |
|  version diff    ||   version  diff    |                               
==========================================
|                  ||                    |
|     CONTENT      ||       CONTENT      |
|                  ||                    |

As the Category1, Category2, Col 1, Col2, Col3, Col4 are all the table header. but Cateogry1, Cateogry2 are merged to make the table more clear.

My xml input like this,

<?xml version="1.0" encoding="utf-8"?>
<Index>
  <entry name="Entry1">
    <entry name="V1.baseversion">1.0.0.77</entry>
    <entry name="V1.sizedifferencescount">0</entry>
    <entry name="V2.sizedifferencescount">0</entry>
    <entry name="V2.baseversion">1.0.0.75</entry>
  </entry>
  <entry name="Entry2">
    <entry name="V1.baseversion">3.0.0.12</entry>
    <entry name="V1.sizedifferencescount">0</entry>
    <entry name="V2.sizedifferencescount">0</entry>
    <entry name="V2.baseversion">3.0.0.13</entry>
  </entry>
 </Index>

Below is what I have tried the xslt so far, I intend to use this to parse the header.

 <xsl:when test="position()=1">
    <xsl:for-each  select="*" > 
        <xsl:sort select="@name"/>
        <xsl:if test=" ( (@name='V1.baseversion') or (@name='V1.sizedifferencescount') or (@name='V2.baseversion') or (@name='V2.sizedifferencescount'))">
            <tr>
                <th>
                    <xsl:attribute name="colspan">
                        <xsl:value-of select="2" />
                    </xsl:attribute>
                    <!--need to print Version 1 here-->
                </th>
                <th>
                    <xsl:attribute name="colspan">
                        <xsl:value-of select="2" />
                    </xsl:attribute>
                    <!--need to print Version 2 here-->
                </th>
            </tr>
            <tr>
                <th>
                    <!--need to print V1.baseversion here-->
                </th>
                <th>
                    <!--need to print V1.sizedifferencescount here-->
                </th>       
                <th>
                    <!--need to print V2.baseversion here-->
                </th>
                <th>
                    <!--need to print V2.sizedifferencescount here-->
                </th>                                       
            </tr>                                 
        </xsl:if>
    </xsl:for-each>
</xsl:when>

And in the output html, each entry in the xml will be one row in the table. The output html should be like this.

<table>
    <tr>
        <th colspan=2>V1</th>
        <th colspan=2>V2</th>

    </tr>
    <tr>
        <th >Version</th>
        <th >Diff</th>
        <th >Version</th>
        <th >Diff</th>
    </tr>
    <tr>
        <td>XXXX</td><!--Display value for Entry1.V1.baseversion-->
        <td>XXXX</td><!--Display value for Entry1.V1.sizedifferencescount-->
        <td>XXXX</td><!--Display value for Entry1.V2.baseversion-->
        <td>XXXX</td><!--Display value for Entry1.V2.sizedifferencescount-->
    </tr>
    <tr>
        <td>XXXX</td><!--Display value for Entry2.V1.baseversion-->
        <td>XXXX</td><!--Display value for Entry2.V1.sizedifferencescount-->
        <td>XXXX</td><!--Display value for Entry2.V2.baseversion-->
        <td>XXXX</td><!--Display value for Entry2.V2.sizedifferencescount-->
    </tr>
</table>

Thank you for your information and help.

Alex

5
  • 1
    Please include some input XML, your required output and what have you tried so far. Commented Jun 9, 2014 at 8:18
  • Thank you for bringing this out! I have update with my input XML and what I have tried so far in this post. Thank you again. Commented Jun 9, 2014 at 11:10
  • Can you also show the actual HTML you expect from the XML you have given too, just so there are no misunderstandings? Thanks! (By the way, your XML is not quite well-formed, you have an opening "index" tag, but a closing "xml" tag) Commented Jun 9, 2014 at 12:33
  • The example is too ambiguous. Will there always be exactly two version under each Test? And how are two (or more?) entries represented in the table? Does the header continue to expand to the right, or should each Test get its own table? Commented Jun 9, 2014 at 13:06
  • Thank you guys! I made more updates, and hope this will be no ambiguous. Thank you again for your time and help!! Commented Jun 9, 2014 at 14:06

1 Answer 1

0

I still think there is something missing in your description, but in the interest of moving this forward, how about:

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <table>
        <tr>
            <th colspan="2">V1</th>
            <th colspan="2">V2</th>
        </tr>
        <tr>
            <th>Version</th>
            <th>Diff</th>
            <th>Version</th>
            <th>Diff</th>
        </tr>
        <xsl:for-each select="Index/entry">
            <tr>
                <td><xsl:value-of select="entry[@name='V1.baseversion']"/></td>
                <td><xsl:value-of select="entry[@name='V1.sizedifferencescount']"/></td>
                <td><xsl:value-of select="entry[@name='V2.baseversion']"/></td>
                <td><xsl:value-of select="entry[@name='V2.sizedifferencescount']"/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.