xml 如何在不替换单个空格的情况下修剪 XSLT 中的空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13974247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I trim space in XSLT without replacing repating whitespaces by single ones?
提问by Mathias Bader
The function normalize-spacereplaces sequences of whitespaces by a single space and trims the provided string. How can I only trim the string without the replacement of whitespaces? There are proprietary solutions like orcl:left-trim, but I am looking for a non-proprietary one.
该函数normalize-space用单个空格替换空格序列并修剪提供的字符串。如何只修剪字符串而不替换空格?有像 的专有解决方案orcl:left-trim,但我正在寻找非专有的解决方案。
Example:
例子:
<xsl:value-of select="trim(/Car/Description)"/>
should turn
应该转
<car>
<description> To get more information look at: www.example.com </description>
</car>
into
进入
"To get more information look at: www.example.com"
采纳答案by J?rn Horstmann
A solution using just xslt 1.0 templates:
仅使用 xslt 1.0 模板的解决方案:
<xsl:variable name="whitespace" select="'	 '" />
<!-- Strips trailing whitespace characters from 'string' -->
<xsl:template name="string-rtrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:variable name="length" select="string-length($string)" />
<xsl:if test="$length > 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, $length, 1))">
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="substring($string, 1, $length - 1)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading whitespace characters from 'string' -->
<xsl:template name="string-ltrim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:if test="string-length($string) > 0">
<xsl:choose>
<xsl:when test="contains($trim, substring($string, 1, 1))">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="substring($string, 2)" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!-- Strips leading and trailing whitespace characters from 'string' -->
<xsl:template name="string-trim">
<xsl:param name="string" />
<xsl:param name="trim" select="$whitespace" />
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string">
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="trim" select="$trim" />
</xsl:call-template>
</xsl:template>
Test code:
测试代码:
<ltrim>
<xsl:call-template name="string-ltrim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</ltrim>
<rtrim>
<xsl:call-template name="string-rtrim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</rtrim>
<trim>
<xsl:call-template name="string-trim">
<xsl:with-param name="string" select="' test '" />
</xsl:call-template>
</trim>
Output:
输出:
<test>
<ltrim>test </ltrim>
<rtrim>
test</rtrim>
<trim>test</trim>
</test>
回答by Dimitre Novatchev
Using FXSL(open source library for XSLT functional programming, written entirely in XSLT) one simply writes:
使用FXSL(用于 XSLT 函数式编程的开源库,完全用 XSLT 编写)只需简单地编写:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="trim.xsl"/>
<xsl:output method="text"/>
<xsl:template match="/*/description">
'<xsl:call-template name="trim">
<xsl:with-param name="pStr" select="."/>
</xsl:call-template>'
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
当此转换应用于提供的 XML 文档时:
<car>
<description> To get more information look at: www.example.com </description>
</car>
the wanted, correct result is produced:
产生了想要的、正确的结果:
'To get more information look at: www.example.com'
How does the trimtemplate work?
trim模板如何工作?
It trims the left leading whitespace, then it reverses the resulting string and trims its leading whitespace, then it finally reverses the resulting string.
它修剪左前导空白,然后反转结果字符串并修剪其前导空白,最后反转结果字符串。
II. XPath 2.0 solution:
二、XPath 2.0 解决方案:
Use:
使用:
replace(replace(/*/description, '^\s*(.+?)\s*$', ''), '^ .*$', '')
Here is an XSLT - 2.0 - based verification:
这是一个基于 XSLT - 2.0 - 的验证:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
"<xsl:sequence
select="replace(replace(/*/description, '^\s*(.+?)\s*$', ''), '^ .*$', '')"/>"
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document (above), the XPath expression is evaluated and the result of this evaluation is copied to the output:
当此转换应用于提供的 XML 文档(上图)时,将计算 XPath 表达式并将此计算的结果复制到输出:
"To get more information look at: www.example.com"
回答by softarchsolutions
normalize-space(actualSting) - This will do it.
normalize-space(actualSting) - 这会做到。
回答by Nik Developer
A very short solution with XSLT1:
XSLT1 的一个非常简短的解决方案:
<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>
回答by toddmo
If you don't have any spaces in the middle, you can simply use:
如果中间没有任何空格,则可以简单地使用:
translate(/Car/Description,' ','')

