Difference between revisions of "M2T-JET-FAQ/How do I put quotes in an XPath string?"

From Eclipsepedia

Jump to: navigation, search
(New page: == Question == 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). == Answer == As of JET 0.9, y...)
 
 
Line 25: Line 25:
 
<c:setVariable var="var" select="  '\"ABC\"'  "/>
 
<c:setVariable var="var" select="  '\"ABC\"'  "/>
 
</pre>
 
</pre>
 +
 +
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>
 
</pre>
  

Latest revision as of 11:48, 13 March 2009

[edit] Question

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

[edit] Answer

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)"/>