Developer
Developer

Reputation: 8636

Create a dynamic XSL based on XML

I am having my XML as follows

<?xml version="1.0" encoding="utf-8"?>
<DeploymentReport xmlns="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02">
<Alerts><Alert Name="DataMotion"><Issue Value="Some value" /><Issue Value="Table" /></Alert><Alert Name="DataIssue"><Issue Value="Actual description" Id="1" /><Issue Value="Actual description." Id="2" /></Alert></Alerts>
<Operations>
<Operation Name="Create">
<Item Value="[dbo].[tblEmployee].[IX_tblEmployee]" Type="SqlIndex" />
<Item Value="[dbo].[TestProc]" Type="SqlProcedure" />
<Item Value="[dbo].[TestProc1]" Type="SqlProcedure" />
</Operation>
</Operations>
</DeploymentReport>

I need to generate a dynamic XSL file or an XSL file which will show all Type in one html Table can some one help me I tried some thing as below which didn't worked

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Table</th>
          </tr>
          <xsl:for-each select="Operation/Item/Value">
            <tr>
              <td><xsl:value-of select="Value"/></td>
              <td><xsl:value-of select="Value"/></td>
              <td><xsl:value-of select="Value"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Result of XSL I am expecting is to display as below all Types under on tr td enter image description here

Rough idea on html

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:bar="http://www.bar.org" xmlns:foo="http://www.foo.org/">
   <body>
      <table border="1">
         <tr>
         <th>SqlProcedure</th>
         </tr>
         <tr>
            <td>TestProc</td>
         </tr>
         <tr>
            <td>TestProc1</td>
            </tr>
         <tr>
         <th>SqlIndex</th></tr>
         <tr>
            <td>IX_tblEmployee</td>

         </tr>
      </table>
   </body>
</html>

Upvotes: 1

Views: 267

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

So you first want to group by @Type, assuming you really need to use XSLT 1.0 then use Muenchian grouping:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:report="http://schemas.microsoft.com/sqlserver/dac/DeployReport/2012/02" exclude-result-prefixes="report">
  <xsl:key name="type" match="report:Item" use="@Type"/>

  <xsl:template match="/">
    <html>
      <body>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Table</th>
          </tr>
          <xsl:for-each select="//report:Operation/report:Item[generate-id() = generate-id(key('type', @Type)[1])]">
            <tr>
              <td>
                  <xsl:value-of select="@Type"/>
              </td>
            </tr>
            <xsl:for-each select="key('type', @Type)">
                <tr>
                    <td>
                        <xsl:value-of select="@Value"/>
                    </td>
                </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 3

Related Questions