XSL substring and indexOf

后端 未结 6 1062
说谎
说谎 2021-02-07 00:14

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\

6条回答
  •  死守一世寂寞
    2021-02-07 01:03

    Here is some one liner xpath 1.0 expressions for IndexOf( $text, $searchString ):

    If you need the position of the FIRST character of the sought string, or 0 if it is not present:

    contains($text,$searchString)*(1 + string-length(substring-before($text,$searchString)))
    

    If you need the position of the first character AFTER the found string, or 0 if it is not present:

    contains($text,$searchString)*(1 + string-length(substring-before($text,$searchString)) + string-length($searchString))
    

    Alternatively if you need the position of the first character AFTER the found string, or length+1 if it is not present:

    1 + string-length($right) - string-length(substring-after($right,$searchString))
    

    That should cover most cases that you need.

    Note: The multiplication by contains( ... ) causes the true or false result of the contains( ... ) function to be converted to 1 or 0, which elegantly provides the "0 when not found" part of the logic.

提交回复
热议问题