创建动态XSL并生成html

时间:2017-05-04 09:24:48

标签: xml xslt

此处需要改进同一问题Create a dynamic XSL based on XML

我的XML如下所示

<?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

<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>

预期的html应如下

<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>

1 个答案:

答案 0 :(得分:1)

我认为您不需要为Alert问题进行分组。看起来您只想输出由&#34;项目/问题&#34;引用的警报问题。记录。

所以,你可以做的是定义一个像查找项目/问题&#34;记录按身份。

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

然后,要仅获取带有引用Alert元素的Issue元素,您可以执行此操作...

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

在此范围内,您可以解决类似的问题

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

对于Operation元素的列表,您现在看起来想要按父操作名称和问题类型对问题进行分组,因此您需要一个连接键,如下所示:

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

然后,您可以以相同的方式使用它。试试这个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>