XSLT 1.0:如何从字符串中只删除一个空格?
XSLT 1.0: How to remove only one whitespace from a string?
例如,我输入了 000 FF0 F4F CV1
这样的字符串。此字符串的输出应为 000FF0F4F CV1
。如您所见,只有一个空白字符被删除:如果有一个空白 - 根本没有空白;如果有两个空格 - 一个被移除,一个被留下(如果有三个空格 - 一个被移除,两个被留下,依此类推)。
更多示例:
000 FF0 F4F CV1
-> 000 FF0 F4FCV1
000 FF0 F4F CV1
-> 000FF0 F4FCV1
现在输入字符串有指定格式,你不知道长度和空格数。唯一的要求是删除一个空格。因此需要通用解决方案。是否知道如何使用 XSLT 1.0 实现它?
如果 - 看起来 - 你想从每个 组 连续的白色 space 字符(包括一组 1)中删除一个字符,你可以这样做类似于:
XML
<root>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string">
<xsl:copy>
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:value-of select="substring-before($text, ' ')"/>
<xsl:variable name="tail" select="substring-after($text, ' ')"/>
<xsl:variable name="first-char" select="substring(normalize-space($tail), 1, 1)" />
<xsl:value-of select="substring-before($tail, $first-char)"/>
<!-- recursive call -->
<xsl:call-template name="process">
<xsl:with-param name="text" select="concat($first-char, substring-after($tail, $first-char))" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<string>000FF0F4F CV1</string>
<string>000 FF0 F4FCV1</string>
<string>000FF0 F4FCV1</string>
</root>
请注意,这假定任何一组连续的白色space 字符中的第一个字符是space。并且该字符串不以白色 space 字符结尾。
例如,我输入了 000 FF0 F4F CV1
这样的字符串。此字符串的输出应为 000FF0F4F CV1
。如您所见,只有一个空白字符被删除:如果有一个空白 - 根本没有空白;如果有两个空格 - 一个被移除,一个被留下(如果有三个空格 - 一个被移除,两个被留下,依此类推)。
更多示例:
000 FF0 F4F CV1
-> 000 FF0 F4FCV1
000 FF0 F4F CV1
-> 000FF0 F4FCV1
现在输入字符串有指定格式,你不知道长度和空格数。唯一的要求是删除一个空格。因此需要通用解决方案。是否知道如何使用 XSLT 1.0 实现它?
如果 - 看起来 - 你想从每个 组 连续的白色 space 字符(包括一组 1)中删除一个字符,你可以这样做类似于:
XML
<root>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
<string>000 FF0 F4F CV1</string>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string">
<xsl:copy>
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, ' ')">
<xsl:value-of select="substring-before($text, ' ')"/>
<xsl:variable name="tail" select="substring-after($text, ' ')"/>
<xsl:variable name="first-char" select="substring(normalize-space($tail), 1, 1)" />
<xsl:value-of select="substring-before($tail, $first-char)"/>
<!-- recursive call -->
<xsl:call-template name="process">
<xsl:with-param name="text" select="concat($first-char, substring-after($tail, $first-char))" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<string>000FF0F4F CV1</string>
<string>000 FF0 F4FCV1</string>
<string>000FF0 F4FCV1</string>
</root>
请注意,这假定任何一组连续的白色space 字符中的第一个字符是space。并且该字符串不以白色 space 字符结尾。