Rendering XML in readable format in HTML

Posted on 2006-03-31
Last Modified: 2013-11-19
Hi there,
we have CLOB objects in our database which contains XML data. We are reading this data and rendering our HTML pages with XSLT. Since the XML in this case is a data value, it comes encoded in our XML payload (&lt; &gt; instead of '<' and '>'). This is because we are using JAXB to handle the marshalling of our data objects and it encodes the data because it needs to produce well formed XML (at least that is what I am guessing)

So the question is - can I use XSLT to transform this encoded data so that I can put, at a minimum carriage returns before each tag?
What is happening currently is the HTML output is correct (<xsl:output method="html" indent="no"/>) but the XML is unreadable since we cannot enforce presentation - i.e. carriage returns and tabs

Any ideas?

Question by:pbarry30
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi pbarry30,

    you would get carriage returns in XSLT by having
    (on two different lines)
    you can also stuff that in a parameter and use it like that

    <xsl:output method="html" indent="yes"/>
    would already give you some indenting

    unless what you are doing is having the XML escaped as pure text
    (it seems from your question you do)

    then the only thing you can do is stuff the XML wit <br /> for linebreaks and &nbsp; for spaces


    Author Comment

    How to I replace say &lt; with <br/>&lt; in XSLT?
    LVL 60

    Accepted Solution

    For that you need a recursive function
    here is an example

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:template match="/">
            <xsl:call-template name="indentTags">
                <xsl:with-param name="argument" select="//yourElement"/>

        <xsl:template name="indentTags">
            <xsl:param name="argument"/>
                <xsl:when test="contains($argument, '&lt;')">
                    <xsl:value-of select="substring-before($argument, '&lt;')"/>
                    <br />
                    <xsl:call-template name="indentTags">
                        <xsl:with-param name="argument" select="substring-after($argument, '&lt;')"/>
                <xsl:otherwise><xsl:value-of select="$argument"/></xsl:otherwise>

    this stylesheet calls out for a named template that calls out for itself until all "&lt;" are processed
    I assume the element //yourElement for replacing
    you can call the template the first time  with the element that contains the pseudeoXML


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    RSS Feeds--IE 13 139
    XML template problem 4 52
    SP to delete duplicates 15 47
    Does a Google search find images with the word being searched? 11 27
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now