Jonathan
Jonathan

Reputation: 11321

XSLT function for escaping quotes?

I have some XSLT that gets an attribute and sends it as part of the URL to a php $_GET variable. It looks like this:

<xsl:attribute name="href">search.php?subject="<xsl:value-of select="@level1"/>"</xsl:attribute> 

It works with most values of @level1. For instance, if the value is foo, I get this url:

search.php?subject="foo"

The problem is, when that value of @level1 contains a quotation mark, like "bar" etc etc it doesn't work. I get this:

search.php?subject=""bar" etc etc"

Which of course returns an empty subject. If I add backslashes, suddenly it works. For instance, if I edit the URL to read:

search.php?subject="\"bar\" etc etc" 

then $_GET['subject]=="bar" etc etc! So how can I get XSL to add backslashes to escape these rogue quotation marks? I tried

<xsl:attribute name="href">search.php?subject="<xsl:value-of select='replace(@level1,",\")'/>"</xsl:attribute> 

and I tried

<xsl:attribute name="href">search.php?subject="<xsl:value-of select="replace(@level1,&quot;,\&quot;"/>"</xsl:attribute>

But nothing seems to work yet.

Upvotes: 1

Views: 237

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

In XSLT 2.0 use the replace() function.

In XSLT 1.0 use the recursive exslt:replace template which you can find at http://www.exslt.org

(And either way, when you ask questions about XSLT, tell us which version you are using).

Upvotes: 2

Related Questions