为什么在 MatLab 中应用转换而不是让 Firefox 处理它时,我最终会得到 8 个额外的空格?
Why do I end up with 8 extra spaces when applying the transform from within MatLab rather than letting Firefox handle it?
我有以下结构:
<suite>
<faults>
<fault componentName="comp1">
<introduceWhen>Time1</introduceWhen>
<signals>
<signal name="sig11" value="1"/>
<signal name="sig22" value="1"/>
</signals>
</fault>
<fault componentName="comp2">
<introduceWhen>Time2</introduceWhen>
<signals>
<signal name="sig44" value="0"/>
</signals>
</fault>
</faults>
</suite>
然后使用以下模板提取一些数据并将其放入设置了 white-space: pre
的 table 单元格中,以便让我将每个故障放在新的一行上。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:output doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN"/>
<xsl:template match="/suite">
<html>
<head>
<title>Space Demo</title>
<style>
td.faults {text-align: left; white-space: pre;}
</style>
</head>
<body>
<table>
<tr>
<td class="faults">
<xsl:apply-templates select="faults"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="faults">
<xsl:for-each select="fault">
<xsl:value-of select="concat(@componentName, ' ', introduceWhen, ' ')"/>
<xsl:for-each select="signals/signal">
<xsl:value-of select="concat(@value, ' ')"/>
</xsl:for-each>
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果我只在 Firefox 中显示 XML,并且将 XSLT 作为样式表,它就会完全按照我的要求工作。但是如果我在 MatLab 中应用转换,生成的 HTML 会在 

之后生成额外的 8 个空格。 MatLab 使用 Saxon 6.5.5 进行 XSLT 处理。
我尝试使用以下模板 trim 字符串,但没有任何区别:
<xsl:template name="trim">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="string-length($str) > 0 and substring($str, 1, 1) = ' '">
<xsl:call-template name="trim">
<xsl:with-param name="str">
<xsl:value-of select="substring($str, 2)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="string-length($str) > 0 and substring($str, string-length($str)) = ' '">
<xsl:call-template name="trim">
<xsl:with-param name="str">
<xsl:value-of select="substring($str, 1, string-length($str)-1)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我该怎么做才能去掉多余的 8 个空格?这里有一个这种行为的工作示例:http://xsltransform.net/jz1PuP4
缩进显然是 Saxon 处理器试图使 HTML 代码更漂亮的尝试。 Xalan 和 libxslt 都不会产生类似的缩进。
由于 HTML 浏览器会忽略多余的白色 space 字符,添加这些字符是无害的,我认为您没有理由担心。
将输出方法更改为 xml
将删除缩进,因为与 HTML 不同,白色 space 可以 是关键的。
我有以下结构:
<suite>
<faults>
<fault componentName="comp1">
<introduceWhen>Time1</introduceWhen>
<signals>
<signal name="sig11" value="1"/>
<signal name="sig22" value="1"/>
</signals>
</fault>
<fault componentName="comp2">
<introduceWhen>Time2</introduceWhen>
<signals>
<signal name="sig44" value="0"/>
</signals>
</fault>
</faults>
</suite>
然后使用以下模板提取一些数据并将其放入设置了 white-space: pre
的 table 单元格中,以便让我将每个故障放在新的一行上。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:output doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:output doctype-public="-//W3C//DTD HTML 4.01//EN"/>
<xsl:template match="/suite">
<html>
<head>
<title>Space Demo</title>
<style>
td.faults {text-align: left; white-space: pre;}
</style>
</head>
<body>
<table>
<tr>
<td class="faults">
<xsl:apply-templates select="faults"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="faults">
<xsl:for-each select="fault">
<xsl:value-of select="concat(@componentName, ' ', introduceWhen, ' ')"/>
<xsl:for-each select="signals/signal">
<xsl:value-of select="concat(@value, ' ')"/>
</xsl:for-each>
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果我只在 Firefox 中显示 XML,并且将 XSLT 作为样式表,它就会完全按照我的要求工作。但是如果我在 MatLab 中应用转换,生成的 HTML 会在 

之后生成额外的 8 个空格。 MatLab 使用 Saxon 6.5.5 进行 XSLT 处理。
我尝试使用以下模板 trim 字符串,但没有任何区别:
<xsl:template name="trim">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="string-length($str) > 0 and substring($str, 1, 1) = ' '">
<xsl:call-template name="trim">
<xsl:with-param name="str">
<xsl:value-of select="substring($str, 2)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="string-length($str) > 0 and substring($str, string-length($str)) = ' '">
<xsl:call-template name="trim">
<xsl:with-param name="str">
<xsl:value-of select="substring($str, 1, string-length($str)-1)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我该怎么做才能去掉多余的 8 个空格?这里有一个这种行为的工作示例:http://xsltransform.net/jz1PuP4
缩进显然是 Saxon 处理器试图使 HTML 代码更漂亮的尝试。 Xalan 和 libxslt 都不会产生类似的缩进。
由于 HTML 浏览器会忽略多余的白色 space 字符,添加这些字符是无害的,我认为您没有理由担心。
将输出方法更改为 xml
将删除缩进,因为与 HTML 不同,白色 space 可以 是关键的。