Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XSLT

Pretty-view XML

5.00/5 (5 votes)
2 Aug 2017CPOL 9.5K  
Creating a pretty-view XML for web applications (or other needs)

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

C#
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));
    // CreatePDFHelper makes PDF routine which can create PDFs from HTML
    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:

XML
<?xml version="1.0" encoding="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'>&lt;!DOCTYPE html&gt;</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;">&lt;<xsl:value-of select="local-name()"/>&gt;
                </div>
                <div style="margin-left: 8px;">
                <xsl:apply-templates select="node()"/>
                </div>
                <div style="font-weight: normal; 
                color: #323296;">&lt;/<xsl:value-of select="local-name()"/>&gt;
                </div>
            </xsl:when>
            <xsl:otherwise>
                <div style="font-weight: normal; color: #643232;">&lt;
                <xsl:value-of select="local-name()"/>&gt;
                <span style="font-weight: bold; color: #326432;">
                <xsl:value-of select="text()"/></span>
                &lt;/<xsl:value-of select="local-name()"/>&gt;</div>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)