如何使用 xslt 中分隔符的索引解析分隔字符串?
我有一个分隔字符串,如下所示,
FirstName&LastName&Address1&Address2&City&State&country&
我需要根据分隔符出现的索引来拆分字符串。
比如说,如果我需要字符串中的前四个值,我应该得到输出 两个不同的变量,其中第一个变量应具有字符串 前四个值和第二个变量具有剩余的字符串。
我不想使用 exslt 或任何其他扩展。 请帮我一个简单的解决方案。
Input:
<xsl:variable name="index" select='4' />
<xsl:variable name="delimitedString" select='FirstName&LastName&Address1&Address2&City&State&country&' />
Output:
<xsl:variable name="requiredString">
FirstName&LastName&Address1&Address2&
</xsl:variable>
<xsl:variable name="remainingString">
City&State&country&
</xsl:variable>
I have a delimited string which looks like below
FirstName&LastName&Address1&Address2&City&State&country&
I need to split the string based on the index of occurrence of the delimiter.
Say, if i need the first four values in the string, i should get the output in
two different variables, where the first variable should have the string with
first four values and the second variable having the remaining string.
I don't want to use exslt or any other extensions.
Please help me with a simple solution.
Input:
<xsl:variable name="index" select='4' />
<xsl:variable name="delimitedString" select='FirstName&LastName&Address1&Address2&City&State&country&' />
Output:
<xsl:variable name="requiredString">
FirstName&LastName&Address1&Address2&
</xsl:variable>
<xsl:variable name="remainingString">
City&State&country&
</xsl:variable>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此转换:
应用于此 XML 文档时:
生成所需的两个字符串,这些字符串是通过在以下位置拆分
/t/text()
得到的:第四个&
分隔符:您可以定义一个变量,例如
vrtfSplitResult
,其主体是将模板应用于包含字符串的文本节点的结果。然后,如果您可以使用xxx:node-set()
扩展,则您要定义的两个变量是:如果您甚至不允许使用
xxx: node-set()
扩展函数,那么您应该使用类似的命名模板:getStartingString
。这与上面splitAt
模板的逻辑非常相似:当此转换应用于同一个 XML 文档时,会再次生成所需的正确结果:
This transformation:
when applied on this XML document:
produces the required two strings that result from splitting
/t/text()
at the fourth&
delimiter:You can define a variable, say
vrtfSplitResult
whose body is the result of applying templates to the text node containing the string. Then, if you can use thexxx:node-set()
extension, the two variables you want to define are:In case you are not allowed to use even the
xxx:node-set()
extension function, then you should use a similar, named template:getStartingString
. This has pretty much the logic of thesplitAt
template above:when this transformation is applied on the same XML document, the wanted, correct result is produced again: