Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

xml - Create a dynamic XSL and generate html

Need improvement for the same question here 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="[dbo].[table]" />
        <Issue Value="[dbo].[table1]" />
    </Alert>
    <Alert Name="DataIssue">
        <Issue Value="The column [table].[Columname] on table [dbo].[table] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option." Id="1" />
        <Issue Value="The column [table1].[Columname] on table [dbo].[table1] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option." Id="2" />
    </Alert>
</Alerts>
<Operations>
    <Operation Name="Drop">
        <Item Value="[dbo].[tbl].[IX_id]" Type="SqlIndex" />
    </Operation>
    <Operation Name="TableRebuild">
        <Item Value="[dbo].[tbl]" Type="SqlTable">
            <Issue Id="1" />
        </Item>
        <Item Value="[dbo].[tbl1]" Type="SqlTable">
            <Issue Id="2" />
        </Item>
    </Operation>
    <Operation Name="Create">

    </Operation>
</Operations>
</DeploymentReport>

XSL that was modified by previous answer

<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:key name="value" match="report:Issue" use="@Value"/>
  <xsl:template match="/">
    <html>
      <body>
      <table style="border:1px solid;" align="center">
          <xsl:for-each select="//report:Issue/report:Item[generate-id() = generate-id(key('value', @Value)[1])]">
            <tr style="text-align:center;">
              <td style="color:red">
                  <xsl:value-of select="@Value"/>
              </td>
            </tr>
            <xsl:for-each select="key('value', @Value)">
                <tr>
                    <td>
                        <xsl:value-of select="@Value"/>
                    </td>
                </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
        <table style="border:1px solid;" align="center">
          <xsl:for-each select="//report:Operation/report:Item[generate-id() = generate-id(key('type', @Type)[1])]">
            <tr style="text-align:center;">
              <td style="color:red">
                  <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>

Expected html should be as follows

<table style="border:1px solid;" align="center">
<tr>
<td style="color:red">
    DataIssue
</td>
<tr>
<td>
 1) The column [table].[Columname] on table [dbo].[table] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option
</td>
</tr>
<tr>
<td>
2) The column [table].[Columname] on table [dbo].[table] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option
</td>
</tr>
<td>
N  number) The column [table].[Columname] on table [dbo].[table] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option
</td>
</tr>
<tr>
<td style="color:red">
    Drop - SqlIndex (combination of Operation name and Item type)
</td>
<tr>
<td>
 [dbo].[tbl].[IX_id]
</td>
</tr>
<tr>
<td style="color:red">
    TableRebuild - SqlTable
</td>
<tr>
<td>
 1 ) [dbo].[tbl1]
</td>
</tr>
<tr>
<td>
 2 ) [dbo].[tbl2]
</td>
</tr>
<td style="color:red">
    Create - SqlIndex
</td>
<tr>
<td>
 1 ) [dbo].[index1]
</td>
</tr>
<tr>
<td>
 2 ) [dbo].[index2]
</td>
</tr>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think you need grouping for your Alert issues. It looks like you simply want to output Alert issues which are referenced by "Item/Issue" records.

So, what you can do, is define a key like to look up "Item/Issue" records by id.

<xsl:key name="issue" match="report:Item/report:Issue" use="@Id"/>

Then, to get only Alert elements with referenced Issue elements, you can do this...

 <xsl:for-each select="//report:Alert[report:Issue[key('issue', @Id)]]">

And within this, you can get the issues like so

<xsl:for-each select="report:Issue[key('issue', @Id)]">

As for the listing of the Operation elements, it looks like you now want to group the issues by parent operation name, and by issue type, so you need a concatenated key like so:

<xsl:key name="type" match="report:Item" use="concat(../@Name, '|', @Type)"/>

You can then use it in the same way though. Try this XSLT

<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="concat(../@Name, '|', @Type)"/>
  <xsl:key name="issue" match="report:Item/report:Issue" use="@Id"/>

  <xsl:template match="/">
    <html>
      <body>
      <table style="border:1px solid;" align="center">
          <xsl:for-each select="//report:Alert[report:Issue[key('issue', @Id)]]">
            <tr style="text-align:center;">
              <td style="color:red">
                  <xsl:value-of select="@Name"/>
              </td>
            </tr>
            <xsl:for-each select="report:Issue[key('issue', @Id)]">
                <tr>
                    <td>
                        <xsl:value-of select="@Value"/>
                    </td>
                </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
        <table style="border:1px solid;" align="center">
          <xsl:for-each select="//report:Operation/report:Item[generate-id() = generate-id(key('type', concat(../@Name, '|', @Type))[1])]">
            <tr style="text-align:center;">
              <td style="color:red">
                  <xsl:value-of select="concat(../@Name, ' - ', @Type)"/>
              </td>
            </tr>
            <xsl:for-each select="key('type', concat(../@Name, '|', @Type))">
                <tr>
                    <td>
                        <xsl:value-of select="@Value"/>
                    </td>
                </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...