Using xsl replace function with regular expression to find multiple instances
Having trouble installing Oxygen? Got a bug to report? Post it all here.
-
- Posts: 316
- Joined: Wed Jun 17, 2015 10:19 am
Using xsl replace function with regular expression to find multiple instances
Post by ann.jensen »
Hi,
I am trying to write an XSL template using replace() function to escape occurrences of $ sign within a string, specifically when $ sign exists within opening and closing double brackets e.g. for following input
Here is a $ sign [[Buy $1000 or more and get $100 credit from us]]
I want
Here is a $ sign [[Buy \$1000 or more and get \$100 credit from us]]
returned.
However I don't know how many $ signs will exist within double square brackets.
The following will replace the first occurrence of $ sign within double square brackets and I want to know if there is a way of adding a grouping and multiplier to cause it to work for any number of occurrences in the input string?
Any advice appreciated,
Regards,
Ann
I am trying to write an XSL template using replace() function to escape occurrences of $ sign within a string, specifically when $ sign exists within opening and closing double brackets e.g. for following input
Here is a $ sign [[Buy $1000 or more and get $100 credit from us]]
I want
Here is a $ sign [[Buy \$1000 or more and get \$100 credit from us]]
returned.
However I don't know how many $ signs will exist within double square brackets.
The following will replace the first occurrence of $ sign within double square brackets and I want to know if there is a way of adding a grouping and multiplier to cause it to work for any number of occurrences in the input string?
Code: Select all
<xsl:variable name="escapeDollar" select="replace($originalText, '(\[\[.*?)\$(.*?\]\])', '$1\\\$$2')"/>
Regards,
Ann
-
- Posts: 922
- Joined: Thu May 02, 2019 2:32 pm
Re: Using xsl replace function with regular expression to find multiple instances
Post by chrispitude »
Hi Ann, while you might be able to come up with a clever regex with lookaheads and lookbehinds to do this, it would not be very maintainable.
I think a better solution is to use <xsl:analyze-string> to grab the content inside [[...]] constructs, then do the simple replace inside that:
I think a better solution is to use <xsl:analyze-string> to grab the content inside [[...]] constructs, then do the simple replace inside that:
Code: Select all
<xsl:analyze-string select="." regex="\[\[.*?\]\]">
<xsl:matching-substring>
<xsl:value-of select="replace(., '\$', '\\\$')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
-
- Posts: 316
- Joined: Wed Jun 17, 2015 10:19 am
Re: Using xsl replace function with regular expression to find multiple instances
Post by ann.jensen »
Thank you so much chrispitude for your advice, it works perfectly.
I was not familiar with <xsl:analyze-string> before.
Thanks again,
Ann
I was not familiar with <xsl:analyze-string> before.
Thanks again,
Ann
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service