Skip to main content
Jump to: navigation, search

M2T-JET-FAQ/How do I put quotes in an XPath string?


How do I put single or double quotes into an XPath string?

I'd like to set a JET variable to the value 'ABC' (including the single quotes).


As of JET 0.9, you can make use of XPath quote escaping and/or quote escaping in JET tag attributes.

<%-- using doubled single quotes (') to indicate an literal single quote character --%>
<c:setVariable var="var" select="   '''ABC'''   "/>

To do double quotes ("ABC"), you could do either of the following:

<%-- surround the select attribute value with single quotes
  -- and using doubled double quotes (") to indicate an literal double quote character --%>
<c:setVariable var="var" select='   """ABC"""   '/>

<%-- using \" indicate to the tag attribute parser a literal double quote character.
  -- The XPath parser then needs no escaping --%>
<c:setVariable var="var" select="   '\"ABC\"'   "/>

In fact, this method may just be clearer than all those extra quotes and back slashes. And remember, the JET variables only need to be defined once (in main.jet) for them to be available to all templates. </pre>

In older JET versions, you have to resort to a bit of Java code to define variables containing the quote strings:

context.setVariable("dquote", "\""); // $dquote = a double quote
context.setVariable("squote", "'"); // $squote = a single quote
<c:setVariable var="var" select="concat($squote,'ABC',$squote)"/>
<c:setVariable var="var" select="concat($dquote,'ABC',$dquote)"/>

Back to the top