转义保留字 [英] Escaping reserved words

查看:193
本文介绍了转义保留字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sitecore提供了一种在Sitecore查询中转义包含不喜欢的字符的方式。这些字符包括连字符和空格。为了简化我的生活,我写了一个简单的帮助函数,可以逃避Sitecore查询的每个部分,并且工作正常一段时间:

  public static string EscapePath(string path){
return Regex.Replace(path,@([^ /] +),#$ 1#) .Replace(#*#,*);
}

替换(#*# *)在那里,因为Sitecore不喜欢它,当你将星号包在散列中)。



正如我所说,这个工作了一段时间。今天,我遇到了一个失败的情况:

  EscapePath(/ sitecore / content /西雅图/ OR / 00010046\" ); 

转义的序列看起来无辜:



<$ #code $ / $#$#$############$#$#$
$ b

,但是在Sitecore中查询失败,并显示消息标识符,GUID或*位于第44位。我将问题缩小到查询中的 #OR#,并突然意识到发生了什么。显然Sitecore采用单词 OR ,即使被转义,也就是说你加入了两个或更多的查询(也就是说,保留字 OR )。明显的修复是用 * [@@ name ='OR'] 替换 #OR#的所有实例,而且工作很好。但是,对我来说,看起来像是一个黑客。



我知道这很可能只发生在名为 OR AND ,但是我找不到任何有关 SDN 谈论Sitecore Query中的任何保留字,并且没有提到如何正确地转义查询,除了在哈希中包装查询外。



目前是否有一种标准的转义查询方式,我无法保证不会遇到这个问题?或者更好的是,一份文档中列出了Sitecore Query中的所有保留字?我可能会坚持使用XPath语法,只是处理(记录在案的)边缘案例中的转义,但如果可能,我想坚持使用Sitecore Query。

解决方案

您可以看到将使用以下方法引发此异常的保留字样列表

 code> Sitecore.Data.Query.QueryTokenBuilder.Identifier(string)

本质上是列表:




  • 祖先


  • li>
  • 后裔

  • div


  • 以下

  • mod



  • previous

  • self

  • true

  • xor



在我有限的研究中,我没有找到一种方法来逃避这些关键字,所以你可能想要在这个列表中进行硬编码。


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:

public static string EscapePath(string path){
    return Regex.Replace(path, @"([^/]+)", "#$1#").Replace("#*#", "*");
}

(the Replace("#*#","*") is in there because Sitecore doesn't like it when you wrap the asterisk in hashes).

As I said, this worked fine for a while. Today, I came across a situation where this fails:

EscapePath("/sitecore/content/Seattle/OR/00010046");

The escaped sequence looks innocent enough:

/#sitecore#/#content#/#Seattle#/#OR#/#00010046#

but the query failed within Sitecore with the message Identifier, GUID or "*" expected at position 44. I narrowed the problem down to the #OR# in the query, and suddenly realized what was going on. Apparently Sitecore takes the lone word OR, even when escaped, to mean that you're joining two or more queries together (that is, to be the reserved-word OR). The obvious fix is to replace all instances of #OR# with *[@@name='OR'], and that works just fine. However, that, to me, looks like a hack.

I know that this will most likely only happen with nodes named OR and AND, but I can't find any documentation on the SDN 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.

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.

解决方案

You can see the list of 'reserved' words that will throw this exception on the following method

Sitecore.Data.Query.QueryTokenBuilder.Identifier(string)

Essentially the list is:

  • ancestor
  • and
  • child
  • descendant
  • div
  • false
  • following
  • mod
  • or
  • parent
  • preceding
  • self
  • true
  • xor

In my limited research I didn't see a way to escape these keywords so you may want to hard code around this list.

这篇关于转义保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆