Saturday, September 23, 2017

Generating html pages using XSLT and powershell

Powershell to create HTML files from XSLT template

$xslPath = "C:\Users\ggoudar\Desktop\xslt\template.xslt"
$outputStream = New-Object -TypeName "System.IO.MemoryStream"
$arglist = New-Object -TypeName "System.Xml.Xsl.XsltArgumentList"
$XslTransform  = New-Object -TypeName "System.Xml.Xsl.XslCompiledTransform"
$reader  = New-Object -TypeName "System.IO.StreamReader" -ArgumentList $outputStream
$XslTransform.Load($xslPath)
[xml]$XmlDocument = Get-content "C:\Users\ggoudar\Desktop\xslt\data.xml"
$XslTransform.Transform($XmlDocument,$arglist,$outputStream)
$outputStream.Position = 0L
$transformed =  $reader.ReadToEnd()
$tempFile = "$([System.IO.Path]::GetTempPath())$([Guid]::NewGuid()).html"
$null = Out-File -NoClobber -FilePath $tempFile -InputObject $transformed -Force

XSLT file for generating the summary report

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="no"/>
  <xsl:template match="/">
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <meta charset="utf-8" />
        <title>Report</title>
        <style type="text/css">
        </style>
      </head>
      <body>
        <div class="container-fluid">
          <div class="row">
            <div class="col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 main">
              <div class="panel panel-primary">
                <div class="panel-heading">
                  Summary - IRM
                </div>
                <div class="panel-body">
                  <xsl:for-each select="Report/Subscription">
                    <table class="table table-striped">
                      <tbody>
                        <tr>
                          <td>
                            <xsl:value-of select="@Name"/>
                            <xsl:apply-templates select="ResourceGroup"/>
                          </td>
                        </tr>
                      </tbody>
                    </table>
                  </xsl:for-each>
                </div>
              </div>
            </div>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="ResourceGroup">
    <xsl:variable name="nodes" select="node()"/>
    <table class="table table-striped">
      <tbody>
        <tr>
          <td>
            <xsl:for-each select="@Name">
              <xsl:value-of select="." />
              <xsl:call-template name="DisplayResourceDetails">
                <xsl:with-param name="nodes" select = "$nodes" />
              </xsl:call-template>
            </xsl:for-each>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>
  <xsl:template name = "DisplayResourceDetails" >
    <xsl:param name = "nodes" />
    <table class="table table-striped">
      <thead>
        <tr>
          <th>Resource Type</th>
          <th>Non-Compliant Resources</th>
          <th>Errors</th>
        </tr>
      </thead>
      <tbody>
        <xsl:for-each select="$nodes">
          <tr>
            <td>
              <xsl:value-of select="@Name"/>
            </td>
            <td>
              <xsl:value-of select="count(Resource/Policy[ValidationResult=0])"/>
            </td>
            <td>
              <xsl:value-of select="count(Resource/Policy[ValidationResult=2])"/>
            </td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
  </xsl:template>
</xsl:stylesheet>


XSLT file for generating the detailed report


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="no"/>

  <xsl:template match="/">
    <html lang="en">
      <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <meta charset="utf-8" />
        <title>Report</title>
        <style type="text/css">
        </style>
      </head>
      <body>
        <div class="container-fluid">
          <div class="row">
            <div class="col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 main">
              <div class="panel panel-primary">
                <div class="panel-heading">
                  Report Information
                </div>
                <div class="panel-body">
                  <table class="table table-striped">
                    <tbody>
                      <tr>
                        <th>Date :</th>
                        <td>
                          <xsl:value-of select="/Report/Subscription/@Date" />
                        </td>
                      </tr>
                      <tr>
                        <th>Subscription :</th>
                        <td>
                          <xsl:value-of select="/Report/Subscription/@Name" />
                        </td>
                      </tr>
                      <tr>
                        <th>Resource Group :</th>
                        <td>
                          <xsl:value-of select="/Report/Subscription/ResourceGroup/@Name" />
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 main">
              <div class="panel panel-primary">
                <div class="panel-heading">
                  Summary
                </div>
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th>Resource</th>
                      <th>Resource Type</th>
                      <th>Non-Compliant Rules</th>
                      <th>Errors</th>
                    </tr>
                  </thead>
                  <tbody>
                    <xsl:for-each select="Report/Subscription/ResourceGroup/ResourceType">
                      <xsl:call-template name="DisplayResourceTyperHeader">
                        <xsl:with-param name="nodes" select = "Resource" />
                        <xsl:with-param name="resourceName" select = "@Name" />
                      </xsl:call-template>
                    </xsl:for-each>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
          <xsl:for-each select="Report/Subscription/ResourceGroup/ResourceType">
            <xsl:call-template name="DisplayResourceTypeDetails">
              <xsl:with-param name="nodes" select = "Resource" />
              <xsl:with-param name="resourceName" select = "@Name" />
            </xsl:call-template>
          </xsl:for-each>
        </div>
      </body>
    </html>
  </xsl:template>
  <xsl:template name = "DisplayResourceTyperHeader" >
    <xsl:param name = "nodes" />
    <xsl:param name = "resourceName" />
    <xsl:for-each select="$nodes">
      <tr>
        <td>
          <xsl:value-of select="@Name" />
        </td>
        <td>
          <xsl:value-of select="$resourceName" />
        </td>
        <td>
          <xsl:value-of  select="count(Policy[ValidationResult=0])"/>
        </td>
        <td>
          <xsl:value-of  select="count(Policy[ValidationResult=2])"/>
        </td>
      </tr>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name = "DisplayResourceTypeDetails" >
    <xsl:param name = "nodes" />
    <xsl:param name = "resourceName" />
    <xsl:for-each select="$nodes">
      <div class="row" id="{generate-id(@Name)}">
        <div class="col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1 main">
          <div class="panel panel-primary">
            <div class="panel-heading">
              <b>
                <xsl:value-of select="@Name" />
              </b>
            </div>
            <div class="panel-body">
              <div class="table-responsive">
                <table class="table table-striped">
                  <thead>
                    <tr>
                      <th>Policy</th>
                      <th>Severity</th>
                      <th>Recommendation</th>
                      <th>Message</th>
                    </tr>
                  </thead>
                  <tbody>
                    <xsl:for-each select="Policy">
                      <tr>
                        <td>
                          <xsl:value-of select="@Name" />
                        </td>
                        <td>
                          <xsl:value-of select="Severity" />
                        </td>
                        <td>
                          <xsl:value-of select="Recommendation" />
                        </td>
                        <td>
                          <xsl:value-of select="Message" />
                        </td>
                      </tr>
                    </xsl:for-each>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>


XML file 


<?xml version="1.0" encoding="utf-8"?>
<Report>
  <Subscription Name="Subscription1" Date="19-09-2017 14:31">
    <ResourceGroup Name="ResourceGroup1">
      <ResourceType Name = "Microsoft.Web/sites">
<Resource Name="TestResource">
 <Policy Name="testing">
<Message>Message1</Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>0</ValidationResult>
 </Policy>
 <Policy Name="testing">
<Message>Message2</Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>0</ValidationResult>
 </Policy>
            </Resource>
<Resource Name="TestResource9">
 <Policy Name="testing123">
<Message>Message3 </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>0</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
      <ResourceType Name = "Microsoft.ServiceFabric/Cluster">
<Resource Name="TestResource1">
 <Policy Name="testing123456">
<Message>Message4</Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>2</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
    </ResourceGroup>
    <ResourceGroup Name="ResourceGroup2">
      <ResourceType Name = "Microsoft.Web/sites">
<Resource Name="TestResource2">
 <Policy Name="testing12390">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>2</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
      <ResourceType Name = "Microsoft.ServiceFabric/Cluster">
<Resource Name="TestResource3">
 <Policy Name="testing12312">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>2</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
    </ResourceGroup>
  </Subscription>
  <Subscription Name="Subscription2">
    <ResourceGroup Name = "ResourceGroup3">
      <ResourceType Name = "Microsoft.Web/sites">
<Resource Name="TestResource4">
 <Policy Name="testing123">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>0</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
      <ResourceType Name = "Microsoft.ServiceFabric/Cluster">
<Resource Name="TestResource5">
 <Policy Name="testing123">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>2</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
    </ResourceGroup>
    <ResourceGroup Name = "ResourceGroup4">
      <ResourceType Name = "Microsoft.Web/sites">
<Resource Name="TestResource6">
 <Policy Name="testing123">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>2</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
      <ResourceType Name = "Microsoft.ServiceFabric/Cluster">
<Resource Name="TestResource7">
 <Policy Name="testing123">
<Message>Expected: 'False'. Received: </Message>
<Recommendation>It is recommended to choose 64-bit platform to handle more load</Recommendation>
<Severity>Medium</Severity>
<Source>Framework</Source>
<ValidationResult>0</ValidationResult>
<ValidationResultText>Not Compliant</ValidationResultText>               
 </Policy>
            </Resource>
      </ResourceType>
    </ResourceGroup>
  </Subscription>
</Report>






No comments:

Post a Comment