I have an xslt code to display an XML file as tree view. The problem is that there are extra line breaks in the generated html list. I added normalize-space to remove extra lines but still there are extra line breaks left. Here is the xslt code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:strip-space elements="*" /> <xsl:template match="comment()"> <li><!--<xsl:value-of select="." disable-output-escaping="yes"/>--></li> </xsl:template> <xsl:template match="comment()[contains(., 'inline-figure')]"> <xsl:copy/> </xsl:template> <xsl:template match="/"> <ul><xsl:apply-templates/></ul> </xsl:template> <xsl:template match="*"> <li><span> <xsl:if test="*"> <xsl:attribute name="class"> <xsl:value-of select="'caret caret-down'"/> </xsl:attribute> </xsl:if> <xsl:value-of select="concat('<',name())"/> <xsl:for-each select="@*"> <xsl:value-of select="concat(' ', name(), '=', '"', ., '"')"/> </xsl:for-each> <xsl:choose> <xsl:when test="text() or *"> <xsl:value-of select="'>'"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="'/>'"/> </xsl:otherwise> </xsl:choose></span> <xsl:if test="text()"> <xsl:value-of select="text()"/> </xsl:if> <xsl:if test="*"> <ul class="nested active"><xsl:apply-templates/></ul> </xsl:if> <xsl:if test="text() or *"> <span><xsl:value-of select="concat('<','/' ,name() , '>')"/></span> </xsl:if></li> </xsl:template> <xsl:template match="*/text()[normalize-space()]"> <xsl:value-of select="normalize-space()"/> </xsl:template> <xsl:template match="*/text()[not(normalize-space())]" /> </xsl:stylesheet>
Here is a sample input:
<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </catalog>
Here is the current output:
<xmp id="example"> <ul><li> <span class="caret caret-down"><catalog></span><ul class="nested active"><li> <span class="caret caret-down"><book id="bk101"></span><ul class="nested active"> <li> <span><author></span>Gambardella, Matthew<span></author></span> </li> <li> <span><title></span>XML Developer's Guide<span></title></span> </li> <li> <span><genre></span>Computer<span></genre></span> </li> <li> <span><price></span>44.95<span></price></span> </li> <li> <span><publish_date></span>2000-10-01<span></publish_date></span> </li> <li> <span><description></span>An in-depth look at creating applications with XML.<span></description></span> </li> </ul> <span></book></span> </li></ul> <span></catalog></span> </li></ul></xmp>
and here is the css i used:
ul { display: inline; margin-bottom: -10px; } li { display: inline; } .caret { display: inline; cursor: pointer; -webkit-user-select: none; /* Safari 3.1+ */ -moz-user-select: none; /* Firefox 2+ */ -ms-user-select: none; /* IE 10+ */ user-select: none; } .caret::before { content: "\25B6"; color: black; display: inline; margin-right: 5px; } .caret-down::before { display: inline; -ms-transform: rotate(90deg); /* IE 9 */ -webkit-transform: rotate(90deg); /* Safari */ } .nested { display: none; } .active { display: block; }
The output looks like this: extra line breaks
I modified just one single line in the XSLT. Everything else stays the same. Please try it.
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>