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?

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

9 Comments

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?
It looks that the issue is with Body and Html tags. Can you please let me know proper location of these 2 tags?
@MilindPatil, Could you clarify your issue?
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.
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.
|

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.