Skip to main content
corrected/clarified statement about index-of() in XPath
Source Link
LarsH
  • 28k
  • 8
  • 98
  • 160

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')

returns 'My name is '.

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.

There is no index-of() function for strings in XPath per se(only for sequences, in XPath 2.0). You can use string-length(substring-before($string, $substring))+1 if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

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')

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

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')

returns 'My name is '.

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.

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.

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

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

updated in response to question edit
Source Link
LarsH
  • 28k
  • 8
  • 98
  • 160

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

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

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

It's not clear exactly what you want to do with the index of a substring (could you provide an example?), but you may be able to use the function substring-after or substring-before:

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

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

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')

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

showed how to emulate index-of.
Source Link
LarsH
  • 28k
  • 8
  • 98
  • 160

It's not clear exactly what you want to do with the index of a substring (could you provide an example?), but you may be able to use the function substring-after or substring-before:

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

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

It's not clear exactly what you want to do with the index of a substring (could you provide an example?), but you may be able to use the function substring-after or substring-before:

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

returns 'My name is '.

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.

There is no index-of() function per se in XPath. There is contains(string, substring). In XPath 2.0 you can use regular expression matching.

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

It's not clear exactly what you want to do with the index of a substring (could you provide an example?), but you may be able to use the function substring-after or substring-before:

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

returns 'My name is '.

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.

There is no index-of() function per se in XPath. You can use string-length(substring-before($string, $substring)) if you specifically need the position (but you'll get a zero-based result, so you need to add 1 if you're going to use the result as the second argument of substring()).

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

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

formatting
Source Link
LarsH
  • 28k
  • 8
  • 98
  • 160
Loading
Source Link
LarsH
  • 28k
  • 8
  • 98
  • 160
Loading