转义保留字

发布于 2024-09-18 06:32:37 字数 1281 浏览 0 评论 0原文

Sitecore 提供了一种转义 Sitecore 查询中包含不喜欢字符的单词的方法。此类字符包括连字符和空格。为了简化我的生活,我编写了一个简单的辅助函数,可以转义 Sitecore 查询的每个部分,并且在一段时间内运行良好:(

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

Replace("#*#","*")< /code> 位于其中,因为 Sitecore 不喜欢将星号包裹在散列中)。

正如我所说,这在一段时间内运作良好。今天,我遇到了一种失败的情况:

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

转义序列看起来很无辜:

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

但在 Sitecore 中查询失败,并显示消息 Identifier, GUID or "*"预计在位置 44。我将问题范围缩小到查询中的#OR#,然后突然意识到发生了什么事。显然,Sitecore 采用单独的单词 OR,即使在转义时也是如此,这意味着您将两个或多个查询连接在一起(即,作为保留字 OR) 。明显的解决方法是将所有 #OR# 实例替换为 *[@@name='OR'],这样效果就很好。然而,对我来说,这看起来像是一个黑客。

我知道这很可能只发生在名为 ORAND 的节点上,但我在 SDN 讨论了 Sitecore 查询中的任何保留字,除了将查询包装在哈希值中之外,没有提及如何正确转义查询。

目前是否有一种标准的方法可以保证我不会遇到这个问题?或者,更好的是,有一份概述 Sitecore 查询中所有保留字的文档?我可能会坚持使用 XPath 语法,只处理(已记录的)边缘情况并转义这些值,但如果可能的话,我想坚持使用 Sitecore 查询。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-09-25 06:32:38

您可以看到将在以下方法中引发此异常的“保留”单词列表

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

本质上该列表是:

  • 祖先
  • 后代
  • div
  • false
  • 下面的
  • mod
  • 父级
  • 前面的
  • self
  • true
  • xor

在我有限的研究中,我没有找到一种方法转义这些关键字,因此您可能需要对此列表进行硬编码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文