XML/XSL将空格替换为<;br/>;
我正在努力清理代码的可读性。使用v1。非常感谢您对我所做错事的任何帮助 我拥有的XML代码:XML/XSL将空格替换为<;br/>;,xml,xslt,Xml,Xslt,我正在努力清理代码的可读性。使用v1。非常感谢您对我所做错事的任何帮助 我拥有的XML代码: ... <trusted-servers>mark mike sally</trusted-servers> ... 最初,它是使用()以这种方式显示的: 我尝试的是: <xsl:value-of select="string-length(substring-before(trusted-servers, concat(' ', trus
...
<trusted-servers>mark mike sally</trusted-servers>
...
最初,它是使用(
)以这种方式显示的:
我尝试的是:
<xsl:value-of select="string-length(substring-before(trusted-servers, concat(' ', trusted-servers, '<xsl:text disable-output-escaping="yes"><![CDATA[<br />]]></xsl:text>')))" data-type="string"/>
但它会抛出一个错误,即未转义字符
假设您拥有xsltproc,这样您就可以执行
<xsl:template match="trusted-servers">
<xsl:for-each select="str:tokenize(., ' ')">
<xsl:if test="position() > 1"><br/></xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
在这里声明xmlns:str=”http://exslt.org/strings“
在xsl:stylesheet
一般来说,应该尽量避免使用
因为您使用的是XSLT 1.0,所以最好的解决方案是编写一个模板,该模板将递归地替换
遇到的第一个空格,例如:
<xsl:template match="trusted-servers" name="replace-space-by-br">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:variable name="head" select="substring-before($text, ' ')"/>
<xsl:variable name="tail" select="substring-after($text, ' ')"/>
<xsl:value-of select="$head"/>
<br/>
<xsl:call-template name="replace-space-by-br">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
什么是目标格式,纯文本、HTML或XML?针对HTML。我正在使用xsltproc将其解析为HTML文件。这是写在一个.xsl文件中的。
<xsl:template match="trusted-servers">
<xsl:for-each select="str:tokenize(., ' ')">
<xsl:if test="position() > 1"><br/></xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
<xsl:template match="trusted-servers" name="replace-space-by-br">
<xsl:param name="text" select="."/>
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:variable name="head" select="substring-before($text, ' ')"/>
<xsl:variable name="tail" select="substring-after($text, ' ')"/>
<xsl:value-of select="$head"/>
<br/>
<xsl:call-template name="replace-space-by-br">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>