Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: Replace single quotes by \'

Tags:

php

xslt

I am using XSLT to transform a XML into a html/php file. In this XSLT I replace some tags by php code and now I have to pass attribute values into that php code. My problem now is that I have to escape single quotes with a backslash to get it work. Is this possible with XSLT.

Example:

<xsl:template match="foo">
    <xsl:processing-instruction name="php">$this->doSomething('<xsl:value-of select="./@bar" />');</xsl:processing-instruction>
</xsl:template>

If I now had a template:

<foo bar="test'xyz"/>

This would generate:

<?php $this->doSomething('test'xyz');?>

What I now want to achieve is the following:

<?php $this->doSomething('test\'xyz');?>

So I want to replace all single quotes by \'

like image 434
stofl Avatar asked Nov 24 '25 09:11

stofl


2 Answers

Use a recursive template to do the find/replace:

<xsl:template name="replace-string">
    <xsl:param name="text"/>
    <xsl:param name="replace"/>
    <xsl:param name="with"/>
    <xsl:choose>
      <xsl:when test="contains($text,$replace)">
        <xsl:value-of select="substring-before($text,$replace)"/>
        <xsl:value-of select="$with"/>
        <xsl:call-template name="replace-string">
          <xsl:with-param name="text"
                          select="substring-after($text,$replace)"/>
          <xsl:with-param name="replace" select="$replace"/>
          <xsl:with-param name="with" select="$with"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Applied to your example:

   <xsl:template match="foo">
    <xsl:processing-instruction name="php">
        <xsl:text>$this->doSomething('</xsl:text>
        <xsl:call-template name="replace-string">
            <xsl:with-param name="text" select="./@bar"/>
            <xsl:with-param name="replace" select='"&apos;"' />
            <xsl:with-param name="with" select='"\&apos;"'/>
        </xsl:call-template>
        <xsl:text>');</xsl:text>
    </xsl:processing-instruction>
</xsl:template>

Note:

  1. The use of <xsl:text> to explicitly define text intended for the output, and not have to worry about whitespace between that text and template calls.
  2. The use of single quotes to enclose the select statement for the replace and with parameters, in order to use double-quotes to indicate a text statement that contains a single quote
  3. The use of the entity reference &apos; for the single quote (a.k.a. apostrophe)
like image 82
Mads Hansen Avatar answered Nov 26 '25 21:11

Mads Hansen


Here's a simpler, inelegant, but quick method for replacing single quotes:

<xsl:variable name="single_quote"><xsl:text>'</xsl:text></xsl:variable>
<xsl:variable name="temp_filename" select="replace($temp_filename,$single_quote,'')"/>

1) Define a variable that contains just an apostrophe. xsl:text is required to get xsl to treat ' as a simple character

2) Use replace function using that variable as the string to match. In this example, I'm simply removing it.

like image 37
AndrewC Avatar answered Nov 26 '25 22:11

AndrewC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!