title: Escaping reserved words
body: <p>Sitecore provides a way of escaping words within a Sitecore query that contain characters that they don't like. Such characters include hyphens and spaces. In the interest of simplifying my life, I wrote a simple helper function that would escape every part of a Sitecore query, and it worked fine for a while:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static string EscapePath(string path){
      return Regex.Replace(path, @"([^/]+)", "#$1#").Replace("#*#", "*");
  }
  </code></pre>
  
  <p>(the <code>Replace("#*#","*")</code> is in there because Sitecore doesn't like it when you wrap the asterisk in hashes).</p>
  
  <p>As I said, this worked fine for a while. Today, I came across a situation where this fails:</p>
  
  <pre class="lang-cs prettyprint-override"><code>EscapePath("/sitecore/content/Seattle/OR/00010046");
  </code></pre>
  
  <p>The escaped sequence looks innocent enough:</p>
  
  <pre><code>/#sitecore#/#content#/#Seattle#/#OR#/#00010046#
  </code></pre>
  
  <p>but the query failed within Sitecore with the message <code>Identifier, GUID or "*" expected at position 44</code>. I narrowed the problem down to the <code>#OR#</code> in the query, and suddenly realized what was going on. Apparently Sitecore takes the lone word <code>OR</code>, even when escaped, to mean that you're joining two or more queries together (that is, to be the reserved-word <code>OR</code>). The obvious fix is to replace all instances of <code>#OR#</code> with <code>*[@@name='OR']</code>, and that works just fine. However, that, to me, looks like a hack.</p>
  
  <p>I know that this will most likely only happen with nodes named <code>OR</code> and <code>AND</code>, but I can't find any documentation on the <a href="http://sdn.sitecore.net/" rel="nofollow noreferrer">SDN</a> that talks about any reserved words within Sitecore Query, and there's no mention on how to properly escape a query, beyond wrapping the query in hashes.</p>
  
  <p>Is there currently a standard way of escaping queries where I'd be guaranteed to not run into this problem? Or, even better, a document out there outlining all the reserved words in Sitecore Query? I could probably stick to the XPath syntax, and just deal with the (documented) edge cases with escaping those values, but I'd like to stick to Sitecore Query if possible.</p>