I want to replace some HTML tags that I have in a CDATA element, but I struggle with getting the syntax in XSLT right. I am getting this error message:
net.sf.saxon.trans.XPathException: Error at character 9 in regular expression '<img(\s+(?![^<>]*alt=['\'])[^<...': expected ()) (line 51)
I guess it does not like the <> inside the regEx. Does anyone knows how to write this in XSLT?
Here is the regEx:
<xsl:variable name='imgTagWithoutAltAttributePattern'> <xsl:text disable-output-escaping='yes'><img(\s+(?![^<>]*alt=['\'])[^<>]+)/></xsl:text></xsl:variable>
I don’t think that the escaped
<>brackets are the source of the problem.Looking at the error message, the error is at char 9, where a closing parentheses
')'is expected:As you can see, the
'<>'comes out just fine. I suspect that the regex engine does not understand the regex in some other way (maybe the negative look-ahead is the problem?).I suggest to try a simpler regex at first, breaking your original one down in different tests to single out the problem:
This way you could inch your way to the cause of the error.
EDIT
By the OP’s own statement, using look-ahead in the regular expression causes the error, so obviously look-ahead is not supported by this regex engine.
To match only
<img>tags that don’t containaltattributes look-around is not absolutely required. I propose a different approach:Credit for this little beast goes to: J.F. Sebastian. Here is the explanation:
The standard disclaimer applies: Regex is not the best tool for processing HTML. Use at your own risk.