Introduction
Need to show XML to user, simplified and colorized - in PDF format - put me into Googling. And no correct answer: how to make pretty-viewable XML?
Background
The main idea is to use XSLT; simple and quick to gain an HTML, which can be shown to user or printed (converted) into PDF format. Since I had a PDF routine which converts HTML into PDF - all I needed was correct XML transformation.
Using the Code
var xmlFile = contentExtractedDir.GetFiles("*.xml").FirstOrDefault();
if (xmlFile != null)
{
XElement contentXml = null;
using (var s = xmlFile.OpenRead())
{
contentXml = XElement.Load(s);
}
xslt = new System.Xml.Xsl.XslCompiledTransform();
xslt.Load(Server.MapPath("App_Data/default.XML.xslt"));
var output = new StringBuilder();
xslt.Transform(contentXml.CreateReader(), null, new StringWriter(output));
var pdfHelper = (new Core(base.Context)).CreatePDFHelper();
var outputPdf = pdfHelper.ConvertHtmlToBytes(output.ToString());
}
And - the main thing that I could't find - the correct XML transformation:
="1.0"="UTF-8"
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40" version="1.0">
<xsl:output method="html" omit-xml-declaration="yes"
encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<meta http-equiv="content-type"
content="text/html; charset=utf-8" />
<title>XML data</title>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="*">
<div style="font-weight: normal;
color: #323296;"><<xsl:value-of select="local-name()"/>>
</div>
<div style="margin-left: 8px;">
<xsl:apply-templates select="node()"/>
</div>
<div style="font-weight: normal;
color: #323296;"></<xsl:value-of select="local-name()"/>>
</div>
</xsl:when>
<xsl:otherwise>
<div style="font-weight: normal; color: #643232;"><
<xsl:value-of select="local-name()"/>>
<span style="font-weight: bold; color: #326432;">
<xsl:value-of select="text()"/></span>
</<xsl:value-of select="local-name()"/>></div>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>