我有一个xslt代码,可将XML文件显示为树状视图。问题是在生成的html列表中有额外的换行符。我添加了normalize-space来删除多余的行,但是仍然有多余的换行符。这是xslt代码:
<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>
这是示例输入:
<?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>
以下是当前输出:
<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>
这是我使用的css:
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;
}
输出看起来像这样: extra line breaks