2

I have simple xml as below

<Scores>
    <Score1>
       <Name>A</Name>
       <Address>Address1</Address>
    </Score1>

    <Score2>
       <Name>B</Name>
       <Address>Address2</Address>
    </Score2>
</Scores>

I want it's output in HTML table as below. (HTML table will have 2 columns headers "Name" and "Address", and i need it's values in rows) I do not want to hardcode "Name" and "Address" headers. They may change in future.

Name            Address
A               Address1
B               Address2

Can you please let me know what will be the XSLT for this?

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
MMP
  • 39
  • 3

1 Answers1

1

Use:

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

    <xsl:template match="/Scores">
        <table>
            <tr>
                <xsl:for-each select="*[1]/*">
                    <th>
                        <xsl:value-of select="local-name()"/>
                    </th>
                </xsl:for-each>
            </tr>
            <xsl:apply-templates select="*"/>
        </table>
    </xsl:template>

    <xsl:template match="*">
        <tr>
            <xsl:for-each select="*">
                <td>
                    <xsl:value-of select="."/>
                </td>
            </xsl:for-each>
        </tr>
    </xsl:template>

</xsl:stylesheet>

Output:

<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
  </tr>
  <tr>
    <td>A</td>
    <td>Address1</td>
  </tr>
  <tr>
    <td>B</td>
    <td>Address2</td>
  </tr>
</table>
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • Hey hi it's working. Thanks for the help. Now the only issue is: the mail is not coming in formatted HTML. Can you please let me know why is it so? – MMP Feb 21 '12 at 13:37
  • It looks that the issue is with Body and Html tags. Can you please let me know proper location of these 2 tags? – MMP Feb 21 '12 at 13:49
  • @MilindPatil, Could you clarify your issue? – Kirill Polishchuk Feb 21 '12 at 13:50
  • I am generating an email using SSIS package. Using your solution, xslt is getting applied properly but the email is not coming in table format. I tried to see source of email and found no table as well as tr, td tags in the email. – MMP Feb 21 '12 at 13:59
  • 2
    This is what happens when someone asks for a solution to a problem, and they are given a solution rather than being given help in writing it themselves. They don't understand the answer, and just come back with more questions. Rather than offering working code, it's much better to find out what people need to learn in order to make progress. – Michael Kay Feb 21 '12 at 13:59
  • Hi Michael Kay, My intention is not to refuse the solution provided by Kirill Polishchuk. I have already accepected it. And trying to understand it further. Just one request, same thing is applicable to you. If you don't want to provide the solution please do not reply. Or in simple sentence you can just ask me to put this question in another forum. I am really thankful to Kirill Polishchuk. But the people like you really demotivative developers. – MMP Feb 21 '12 at 14:05
  • @MilindPatil: It is also good to ask Kirill why despite his answer the weather still hasn't improved. I will remember your name and will always try to avoid answering your questions. – Dimitre Novatchev Feb 21 '12 at 14:19
  • Hi Dimitre Novatchev, please don't be bad with this this good web. Now onwards i don't want to put any comment against my statement. Guys please be helpful not harmful... – MMP Feb 21 '12 at 14:27
  • @Kirill Polishchuk: Can you please take a look at http://stackoverflow.com/questions/30730748/xml-to-html-using-xslt-of-inspect-code-of-vs-projects I am relatively new to this..please help me out – Nevin Raj Victor Jun 09 '15 at 12:00