Escape single quote in xslt concat function
Asked Answered
B

6

59

I want to output single quote around $ID variable in the below xsl:value-of xsl statment.

<xsl:value-of select="concat('process[@Ref=',$ID,']')"></xsl:value-of>

currently it prints

process@Ref=87799989

How can I achieve this?

Byrne answered 22/5, 2010 at 7:19 Comment(2)
Good Question (+1). See my answer for a complete solution. :)Albumin
Good Question (and answered thankfully!) ; this was driving me nuts !Nolanolan
C
34

Use &apos;?

<xsl:value-of select="concat('process[@Ref=&apos;',$ID,'&apos;]')"></xsl:value-of>

Edit: See Dimitre's answer for a better solution.

Catamite answered 22/5, 2010 at 7:21 Comment(4)
It's odd this worked; as &apos; should be decoded equivalent to a literal '. Perhaps a buggy XSLT engine?Anibalanica
Yuo need to use both &apos; and the single quote together: &apos;'Hawkeyed
@Vadi - &apos; and the single quote doesn't seem to work for gmail filters but it worked without the single quote.Rubierubiginous
@Vadi - Thanks a lot for that suggestion. I went crazy trying to escape it.Ballard
A
52

In XPath 1.0:

You can use the built-in entities &apos; and &quot;

In XSLT 1.0:

Alternatively, you can define your $Q and $APOS variables (put the content (the literal " or the literal ' character) in the body of the xsl:variable, not in the select attribute).

In XPath 2.x (this also means XSLT 2.x and XQuery 1.x)

Simply escape an apostrophe by entering two adjacent apostrophes, escape a quote by entering two adjacent quotes, as defined by the XPath 2.0 language

Albumin answered 22/5, 2010 at 14:55 Comment(2)
Could you please provide examples? (one example tells more than 1000 words and pushes your answer to the top). ThanksNumeral
@RichardGomes, See this: https://mcmap.net/q/330977/-xsl-escaping-an-apostrophe-during-xsl-when-test ,or this: https://mcmap.net/q/330978/-pass-href-dynamically-in-an-xml-file , or this: https://mcmap.net/q/330979/-concat-quotation-mark-and-apostrophe-combination-problemsAlbumin
F
40

To expand on Dimitre's answer, you can use this solution in XSLT:

<xsl:variable name="apos">'</xsl:variable>
<xsl:value-of select="concat('process[@Ref=',$apos,$ID,$apos,']')"></xsl:value-of>
Fibrilliform answered 15/6, 2011 at 21:27 Comment(3)
The variable method works and looks the most readable (IMHO).Nolanolan
This is the second time that this solution has saved me wasting hours of trial and error and debugging time.Kist
Seems to work in XSLT 1.0 and 2.0, which I liked because then I can put it into a common.xslt file.Gravitate
C
34

Use &apos;?

<xsl:value-of select="concat('process[@Ref=&apos;',$ID,'&apos;]')"></xsl:value-of>

Edit: See Dimitre's answer for a better solution.

Catamite answered 22/5, 2010 at 7:21 Comment(4)
It's odd this worked; as &apos; should be decoded equivalent to a literal '. Perhaps a buggy XSLT engine?Anibalanica
Yuo need to use both &apos; and the single quote together: &apos;'Hawkeyed
@Vadi - &apos; and the single quote doesn't seem to work for gmail filters but it worked without the single quote.Rubierubiginous
@Vadi - Thanks a lot for that suggestion. I went crazy trying to escape it.Ballard
B
5
<xsl:value-of
select="concat('process[@Ref=&apos;',$ID,'&apos;]')"></xsl:value-of>

this doesn't work for me. My solution is:

<xsl:value-of select="concat(&quot;process[@Ref='&quot;,$oidConstant,&quot;'&quot;)"></xsl:value-of>
Barranquilla answered 7/12, 2010 at 9:43 Comment(1)
I was skeptical but this works - the plain text double quote contains the select statement and the encoded doubles are consumed by the parser - niceOpenhanded
B
0

Easy Example would be

      <xsl:variable name="varTitle" select="title" />
<xsl:variable name="APOS">'</xsl:variable>
<xsl:value-of select="translate($varTitle, 'any text', $APOS)"/>

This will replace "any text" with ' in my title.

Bracey answered 7/3, 2014 at 13:47 Comment(0)
M
0

Instead of using concat(), you can do the string concatenation inside a variable assignment. That allows to write a simple, unescaped ' to mean ':

<xsl:variable name="process-selector">process[@Ref='<xsl:value-of select="$ID"/>']</xsl:variable>
<xsl:value-of select="$process-selector" />

With the recommended way to allow linebreaks without accidentally adding whitespace to the output, this becomes quite lengthy (but that's XSL, right?):

<xsl:variable name="process-selector">
    <xsl:text>process[@Ref='</xsl:text>
    <xsl:value-of select="$ID"/>
    <xsl:text>']</xsl:text>
</xsl:variable>
<xsl:value-of select="$process-selector" />

 


Full XSL file for this solution to test, for example with an online XSL-T service like xsltransform.net:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html"/>

    <xsl:template match="/" name="strip-space" >
        <xsl:variable name="ID" select="123"/>
        <xsl:variable name="process-selector">
            <xsl:text>process[@Ref='</xsl:text>
            <xsl:value-of select="$ID"/>
            <xsl:text>']</xsl:text>
        </xsl:variable>
        <xsl:value-of select="$process-selector" />
    </xsl:template>

</xsl:transform>
Micmac answered 3/7, 2020 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.