Forum Moderators: open

Message Too Old, No Replies

convert one line xml string into well indented text

         

alexis

9:54 pm on Jun 10, 2005 (gmt 0)

10+ Year Member



Do you know of a software or a script that would take a long line of xml or html and output legible indented text?

For example:

<tag1 ID='whatever'><tag2 arg='a'><tag3> Text here <tag4>More text</tag4></tag3></tag2></tag1>

would become:

<tag1 ID='whatever'>
.<tag2 arg='a'>
..<tag3>
...Text here
...<tag4>More text</tag4>
..</tag3>
.</tag2>
</tag1>

(replace the dots by tabs or spaces)

johnl

10:19 pm on Jun 11, 2005 (gmt 0)

10+ Year Member



hi,

the software you are looking for probably already exists in your browser.
when loading xml-data into a modern browser it will apply an internal stylesheet for display which nearly does what you want (it adds a '-' or '+' to some entries though).

alternatively you can supply your own stylesheet specifying


<xsl:output indent="yes"/>

and using an indentity-transform similar to:


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*¦@*¦node()">
<xsl:copy>
<xsl:apply-templates select="*¦@*¦node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

in this untested example you still will have to change unwanted linefeeds to spaces. you could use normalize-space() here.