Escape single quote in xslt concat function

XsltXmlJavascript

Xslt Problem Overview


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

Please let me know how can i achieve this.

Thanks in advance, Keshav

Xslt Solutions


Solution 1 - Xslt

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

Solution 2 - Xslt

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>

Solution 3 - Xslt

Use &apos;?

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

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

Solution 4 - Xslt

<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>

Solution 5 - Xslt

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.

Solution 6 - Xslt

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkeshav84View Question on Stackoverflow
Solution 1 - XsltDimitre NovatchevView Answer on Stackoverflow
Solution 2 - XsltVelozView Answer on Stackoverflow
Solution 3 - XsltkennytmView Answer on Stackoverflow
Solution 4 - XsltSimonView Answer on Stackoverflow
Solution 5 - Xsltuser3392863View Answer on Stackoverflow
Solution 6 - XslttaniusView Answer on Stackoverflow