Regex to remove word from XSLT
I need help writing regex to remove a word using XSLT.
I need to change the output of my XML 开发者_运维问答file's "detailpath" from:
/events/262/26207
...to simply:
262/26207
The XSL is:
<xsl:value-of select="detailpath"/>
How can I remove "/events/"?
Thanks in advance.
The regex pattern would simply be '/events/'
You can use it in an XSLT 2.0 replace() function call:
<xsl:value-of select="replace(detailpath,'/events/','')"/>
function returns the xs:string that is obtained by replacing each non-overlapping substring of $input that matches the given $pattern with an occurrence of the $replacement string.
You can optionally specify flags
fn:replace( $input as xs:string?, $pattern as xs:string, $replacement as xs:string) as xs:string fn:replace( $input as xs:string?, $pattern as xs:string, $replacement as xs:string, $flags as xs:string) as xs:string
Do note that for your example (starting string) you could alse use this XPath 1.0 expression:
substring(detailpath, 9 * starts-with(detailpath,'/events/'))
Another XPath 1.0 solution (assuming there is only one instance of the string to replace) that would work regardless of where the text fragment is in the string:
<xsl:value-of select="concat(substring-before(detailpath,'/events/'),
substring-after(detailpath,'/events/'))" />
echo /events/262/26207 | sed -e 's|/events/||g'
This works in bash on Solaris and, with tweaking, should work in PHP, Javascript and Perl, to my knowledge.
精彩评论