Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Rendering XML in readable format in HTML

Posted on 2006-03-31
3
Medium Priority
?
500 Views
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?
Thanks

Pat
0
Comment
Question by:pbarry30
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16348624
Hi pbarry30,

you would get carriage returns in XSLT by having
<xsl:text>
</xsl:text>
(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

Cheers!
0
 

Author Comment

by:pbarry30
ID: 16351118
How to I replace say &lt; with <br/>&lt; in XSLT?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1000 total points
ID: 16351305
For that you need a recursive function
here is an example

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:call-template name="indentTags">
            <xsl:with-param name="argument" select="//yourElement"/>
        </xsl:call-template>
    </xsl:template>

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

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

cheers
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

575 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