It is a relatively simple operation to use System.Xml.Xsl and System.Xml.XPath to iterate through the elements in the XDocument created by the getXDocument() method. The result is an HTML page that can be displayed in any browser.
The ReportDocument() method prompts for the name of an HTML document to output to.
public void ReportDocument(bool includeRulesets, bool includeIssues)
{
try
rules setsreports, creating{
string title = "";
if (includeRulesets) title += "RuleSets";
if (includeRulesets && includeIssues) title += " and ";
if (includeIssues) title += "Issues";
string shortName = System.IO.Path.GetFileNameWithoutExtension( this.document.FullName);
string fileName = System.IO.Path.Combine(this.document.Path, shortName + ".html");
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.Title = "Save " + title;
dlg.InitialDirectory = System.Environment.GetFolderPath( System.Environment.SpecialFolder.MyDocuments);
dlg.FileName = shortName + " " + title + ".html";
dlg.OverwritePrompt = true;
dlg.DefaultExt = ".html";
dlg.Filter = "HTML documents (.html)|*.html";
if (dlg.ShowDialog() == true)
{
fileName = dlg.FileName;
}
else return;
XDocument xDoc = getXDocument(includeRulesets, includeIssues);
if (xDoc == null)
{
return;
}
//Get the XSL Stylesheet
string xslMarkup = getRuleSetXSL();
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(System.Xml.XmlReader.Create(new StringReader(xslMarkup)));
//Save the XDocument to a temporary file
string tempFile = System.IO.Path.GetTempFileName();
xDoc.Save(tempFile);
// Execute the transform and output the results to html.
xslt.Transform(tempFile, fileName);
//Delete the temporary file
System.IO.File.Delete(tempFile);
//Open in web browser (associated program)
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(fileName);
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
System.Diagnostics.Process.Start(startInfo);
}
catch (Exception)
rules set reportscreating{
throw;
}
}
Getting the XSL stylesheet
The XSL template returned by this method can be saved as a file, say RuleSets.xslt,
and can be used to transform the rule sets in any Visio document saved
in XML format. The output will be a rule set report in HTML.
private string getRuleSetXSL()
{
return @"<?xml version='1.0' encoding='UTF-8' ?> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:vx= 'http://schemas.microsoft.com/visio/2006/extension'
xmlns:v14= 'http://schemas.microsoft.com/office/visio/2009/5/extension'
xmlns='http://schemas.microsoft.com/visio/2003/core'>
<HTML>
<BODY>
<H1>Visio Rules Tools RuleSets Report</H1>
</BODY>
</HTML>
<!--<xsl:template match='text()' />-->
<xsl:template match='/'>
<xsl:apply-templates select='//*/*/*/v14:RuleSet' />
</xsl:template>
<xsl:template match='v14:RuleSet' >
<HTML>
<BODY>
<H1>Visio Rules Tools RuleSets Report</H1>
<xsl:for-each select='.'>
rules set reportsXSL stylesheet, obtaining<TABLE width='100%'>
<TR>
<TABLE width='100%' frame='border' style='font-size:24px'>
<TR style='background-color:teal;color:white; font-weight:bold'>
<TH >ID</TH>
<TH >NameU</TH>
<TH >Name</TH>
<TH >Description</TH>
</TR>
<TR style='background-color:azure'>
<TD>
<xsl:value-of select='@ID'/>
</TD>
rules setsreports, creating<TD>
<xsl:value-of select='@NameU'/>
</TD>
<TD>
<xsl:value-of select='@Name'/>
</TD>
<TD >
<xsl:value-of select='@Description'/>
</TD>
</TR>
</TABLE>
</TR>
<TR>
<TABLE width='100%' frame='border' >
<TR style='font-weight:bold; background-color:gray;color:white;padding:4px'>
<TH>ID</TH>
<TH>NameU</TH>
<TH>Category</TH>
<TH>Description</TH>
<TH>RuleTarget</TH>
<TH>RuleFilter</TH>
<TH>RuleTest</TH>
</TR>
<xsl:for-each select='v14:Rule'>
<TR style='vertical-align:top'>
<TD >
<xsl:value-of select='@ID'/>
</TD>
<TD >
<xsl:value-of select='@NameU'/>
</TD>
<TD >
<xsl:value-of select='@Category'/>
</TD>
<TD >
<xsl:value-of select='@Description'/>
</TD>
<TD >
<xsl:value-of select='@RuleTarget'/>
</TD>
<TD >
<xsl:value-of select='v14:RuleFilter'/>
</TD>
<TD >
<xsl:value-of select='v14:RuleTest'/>
</TD>
</TR>
rules setsreports, creating</xsl:for-each>
</TABLE>
</TR>
</TABLE>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>";
}
Save the main body of the getRuleSetXSL() into a RuleSets.xslt file, then use XML Notepad to open a Visio XML format document.
You can then enter the full path to the RuleSets.xslt file on the XSL Output tab in XML Notepad, and press Transform. The Visio Rules Tools RuleSets Report will then be displayed.
Alternatively add the following line as line 2 in any XML file that contains rule sets (edit the href path accordingly):
<?xml-stylesheet type="text/xsl" href="RuleSets.xslt"?>
Open in your web browser to display the report!