本文介绍了XSL 子串和 indexOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XSLT 的新手.我想知道是否可以选择项目的子字符串.我正在尝试解析 RSS 提要.描述值的文本比我想显示的要多.我想根据某个子字符串的索引来获取它的子字符串.基本上,我想显示传递 indxOf('some_substring') 和长度作为参数的子字符串调用的结果.这可能吗?

I'm new to XSLT. I wonder if it is possible to select a substring of an item. I'm trying to parse an RSS feed. The description value has more text than what I want to show. I'd like to get a subtring of it based on the index of some substring. Basically, I want to show the result of a substring call passing indxOf('some_substring') and a length as parameters. Is this possible?

来自评论:

我想选择一个字符串的文本位于发生之后子串的

推荐答案

目前尚不清楚您想对子字符串的索引做什么 [更新:现在更清楚了 - 谢谢] 但您可以使用函数 substring-aftersubstring-before:

It's not clear exactly what you want to do with the index of a substring [update: it is clearer now - thanks] but you may be able to use the function substring-after or substring-before:

substring-before('My name is Fred', 'Fred')

返回'我的名字是'.

如果您需要更详细的控制,substring() 函数可以使用两个或三个参数:字符串、起始索引、长度.省略长度以获取字符串的其余部分.

If you need more detailed control, the substring() function can take two or three arguments: string, starting-index, length. Omit length to get the whole rest of the string.

XPath 中没有用于字符串的 index-of() 函数(仅用于序列,在 XPath 2.0 中).如果您特别需要位置,可以使用 string-length(substring-before($string, $substring))+1.

There is no index-of() function for strings in XPath (only for sequences, in XPath 2.0). You can use string-length(substring-before($string, $substring))+1 if you specifically need the position.

还有contains($string, $substring).这些都记录在此处.在 XPath 2.0 中,您可以使用正则表达式匹配.

There is also contains($string, $substring). These are all documented here. In XPath 2.0 you can use regular expression matching.

(XSLT 主要使用 XPath 来选择节点和处理值,所以这实际上更多一个 XPath 问题.我是这样标记的.)

(XSLT mostly uses XPath for selecting nodes and processing values, so this is actually more of an XPath question. I tagged it thus.)

这篇关于XSL 子串和 indexOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:00