xslt 2.0 how replace $ by escaped dollar (for conv

2019-07-13 19:59发布

问题:

I am new to XSLT. I googled extensively but couldn't figure out how to do the following:

I am transforming XML to LaTeX. Of course, LaTeX needs to escape characters such as $ and #. I tried the following in the replace function but it does not work. (They do work without the replace function.)

<xsl:template match="xyz:doc">
\subsubsection{<xsl:value-of select="replace( xyz:headline, '(\$)', '\$1' )"/>}  
...
</xsl:template>

<xsl:template match="xyz:doc">
\subsubsection{<xsl:value-of select="replace( xyz:headline, '\$', '\$' )"/>}  
...
</xsl:template>

Possible content to be escaped is: "Locally defined field #931" or "Locally defined subfield $b"

What am I doing wrong? Many thanks for your answers!

回答1:

If you want to replace a dollar symbol $ in the input with \$ in the output then use replace(xyz:headline, '\$', '\\\$').

If there are several characters that need the same escaping then replace(xyz:headline, '([$#])', '\\$1') should do.

Sample at http://xsltransform.net/bdxtqX/1