Click here to Skip to main content
15,886,030 members
Articles / Programming Languages / XSLT
Tip/Trick

Pretty-view XML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
2 Aug 2017CPOL 9.2K   5   2
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)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUseful information Pin
NikStar2-Aug-17 9:36
NikStar2-Aug-17 9:36 
AnswerRe: Useful information Pin
Member 123643903-Aug-17 22:28
Member 123643903-Aug-17 22:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.