XSL Character Substitution ()

I have a node with some data:

<something>Blah blah (Hello World) hihi</something> 

When I execute XSLt, I try to escape from the open and closed brackets and for my life I can’t figure out how to achieve this until now. I'm trying something like this.

 <xsl:variable name="rb">(</xsl:variable> <xsl:message><xsl:value-of select="replace(something, $rb, concat('\\', $rb))" /</xsl:message> 

This is the error I get using Saxon: Error in xsl: row template 728 column something.xml: FORX0002: error in character 1 in regular expression "(": expected ())

thanks

+4
source share
2 answers
 <xsl:variable name='string'>Blah blah (Hello World) hihi</xsl:variable> <xsl:message> <xsl:value-of select="replace($string, '(\(|\))','\\$1')" /> </xsl:message> 

This will work for any bracket. Your code is also incomplete. What it is? Does it contain the expected value? You are missing xsl: value-of at the end.

EDIT: after @Dimitre's comment:

 <xsl:variable name="rb">(</xsl:variable> <xsl:variable name='string'>Blah blah (Hello World) hihi</xsl:variable> <xsl:message> <xsl:value-of select="replace($string, concat('\', $rb), concat('\\', $rb))" /> </xsl:message> 

You will need the above results, although I see no reason to prefer this over my original solution.

+2
source

This works with AltovaXML20011 (XML-SPY) :

 <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:template match="/"> <xsl:sequence select="replace(replace(., '\(', '\\(' ), '\)', '\\)' )"/> </xsl:template> </xsl:stylesheet> 

when applied to the provided XML document :

 <something>Blah blah (Hello World) hihi</something> 

the desired result is obtained :

 Blah blah \(Hello World\) hihi 
+2
source

All Articles