我需要将字符串'abcdef'转换为其部分,'a','b','c','d','e','f'.愚蠢的我尝试了tokenize('abcdef','')但当然返回FORX0003错误(tokenize()中的正则表达式不能是与零长度字符串匹配的表达式).
我实际上是在尝试将字符串最终转换为'a/b/c/d/e/f',因此任何可以让我直接进入此状态的快捷方式也很有用.
(我正在使用Saxon 9.3 for .NET平台)
要从字符串中获取所需的字符序列,请$str使用这对函数string-to-code-points()和codepoints-to-string():
for $c in string-to-codepoints($str)
return
codepoints-to-string($c)
Run Code Online (Sandbox Code Playgroud)
要使用'/'作为连接字符串连接此字符序列,只需应用string-join()上面的表达式.
这是一个完整的代码示例:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:sequence select=
"string-join(
for $c in string-to-codepoints('ABC')
return
codepoints-to-string($c),
'/'
)
"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
生成想要的字符序列:
A/B/C
Run Code Online (Sandbox Code Playgroud)
说明:
string-to-codepoints($str) 产生一系列代码点(将它们视为"字符代码"),代表字符串的每个字符.
例如 ;
string-to-codepoints('ABC')
Run Code Online (Sandbox Code Playgroud)
产生序列:
65 66 67
codepoints-to-string($code-seq)
Run Code Online (Sandbox Code Playgroud)
是反函数string-to-codepoints().给定一系列代码点,它产生字符串,其字符由序列中的代码点表示.从而:
codepoints-to-string((65,66,67))
Run Code Online (Sandbox Code Playgroud)
生成字符串:
ABC
Run Code Online (Sandbox Code Playgroud)
因此:
for $c in string-to-codepoints($str)
return
codepoints-to-string($c)
Run Code Online (Sandbox Code Playgroud)
获取每个字符的代码点$str并将其转换为单独的字符串.
string-join()然后使用我们使用提供的join-character"/"连接所有这些单独的字符串.
使用这一行:
replace(replace($input, "(.)", "$1/", "s"), "(.*).$", "$1", "s")
Run Code Online (Sandbox Code Playgroud)
其中$input指向您的原始字符串。该行的返回是您想要的字符串。
a/b/c/d/e/f
Run Code Online (Sandbox Code Playgroud)