如何检查 xquery 中的字符串是否包含双引号?

发布于 2024-10-19 05:33:52 字数 1245 浏览 2 评论 0原文

我是 xQuery 的新手,尝试在 oracle osb 中编写 xquery 片段,并想检查我的查询项是否由双引号引起来。我有如下代码,但 fn:replace 或 fn:contains 不起作用。 我输入的字符串,qt =“test”,那么它是一个短语如果qt = test,那么它很简单

我怎样才能做到这一点?

if ($qt != "") 

then let $newQt := fn:replace($qt,'"','\\"')

     return
     (    
      if (fn:contains($newQt,'\\"')) 

      then <stan:query> 

              <stan:queryTerm>{
                  fn:concat('string("',
                            $newQt,
                            '", mode="phrase", annotation_class="user")'
                  )
             }</stan:queryTerm>   

             <stan:mode>{'RAW'}</stan:mode>

           </stan:query>

      else <stan:query> 

             <stan:queryTerm>{
                 fn:concat(
                    'string("',
                    $newQt,
                    '", mode="simpleany", annotation_class="user")'
                 )
             }</stan:queryTerm>   

             <stan:mode>{'RAW'}</stan:mode>

           </stan:query>

     )

else <stan:query> 

       <stan:queryTerm>{$newQt}</stan:queryTerm>

       <stan:mode>{'AND'}</stan:mode>

     </stan:query>

I am new to xQuery , trying to write xquery snippets in oracle osb and want to check if my query term is bounded by double quote. I have the code like below, but fn:replace or fn:contains is not working.
My input string , qt = "test", then it is a phrase if qt = test, then it is simple

How can I make this work ?

if ($qt != "") 

then let $newQt := fn:replace($qt,'"','\\"')

     return
     (    
      if (fn:contains($newQt,'\\"')) 

      then <stan:query> 

              <stan:queryTerm>{
                  fn:concat('string("',
                            $newQt,
                            '", mode="phrase", annotation_class="user")'
                  )
             }</stan:queryTerm>   

             <stan:mode>{'RAW'}</stan:mode>

           </stan:query>

      else <stan:query> 

             <stan:queryTerm>{
                 fn:concat(
                    'string("',
                    $newQt,
                    '", mode="simpleany", annotation_class="user")'
                 )
             }</stan:queryTerm>   

             <stan:mode>{'RAW'}</stan:mode>

           </stan:query>

     )

else <stan:query> 

       <stan:queryTerm>{$newQt}</stan:queryTerm>

       <stan:mode>{'AND'}</stan:mode>

     </stan:query>

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

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

发布评论

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

评论(2

—━☆沉默づ 2024-10-26 05:33:52

这个问题很难回答,因为我无法重复您的问题(我根本无法轻松地让您的代码运行)。我也没有使用过“oracle osb”,所以我不确定这是否会有很大帮助。

我能够使用以下 xquery 完成我认为您正在尝试执行的操作。 (我的处理器是 Saxon-HE XQuery 9.3.0.4。我不必在函数上使用 fn: 前缀,但也许您在 Oracle 中这样做?)

XQuery :

declare namespace stan = "http://notsurewhatthisis.com/stan";

let $qt := '"TEST"'
let $newQt := replace($qt,'"','\\"')
let $query :=
  if ($qt != '')
  then
    if (contains($qt,'"')) 
    then
      <stan:query>
        <stan:queryTerm type="phrase">{concat('string("',$newQt,'", mode="phrase", annotation_class="user")')}</stan:queryTerm>
        <stan:mode>RAW</stan:mode>
      </stan:query>
    else
      <stan:query>
        <stan:queryTerm type="phrase">{concat('string("',$newQt,'", mode="simpleany", annotation_class="user")')}</stan:queryTerm>
        <stan:mode>RAW</stan:mode>
      </stan:query>
  else
    <stan:query> 
      <stan:queryTerm>{$newQt}</stan:queryTerm>
      <stan:mode>AND</stan:mode>
    </stan:query>
return
  $query

输出:

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm type="phrase">string("\"TEST\"", mode="phrase", annotation_class="user")</stan:queryTerm>
   <stan:mode>RAW</stan:mode>
</stan:query>

输出 (将 $qt 更改为 let $qt := 'TEST' 后):

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm type="phrase">string("TEST", mode="simpleany", annotation_class="user")</stan:queryTerm>
   <stan:mode>RAW</stan:mode>
</stan:query>

output (将 $qt 更改为 let $qt := '' 后,这就是您的 if ($qt != "") 然后) 会很吸引):

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm/>
   <stan:mode>AND</stan:mode>
</stan:query>

希望这会有所帮助。

This question is hard to answer because I can't duplicate your issue (I can't easily get your code to run at all). I also haven't used "oracle osb" so I'm not sure if this is going to be much help at all.

I was able to accomplish what I think you're trying to do using the following xquery. (My processor is Saxon-HE XQuery 9.3.0.4. I don't have to use the fn: prefixes on the functions, but maybe you do in oracle?)

XQuery:

declare namespace stan = "http://notsurewhatthisis.com/stan";

let $qt := '"TEST"'
let $newQt := replace($qt,'"','\\"')
let $query :=
  if ($qt != '')
  then
    if (contains($qt,'"')) 
    then
      <stan:query>
        <stan:queryTerm type="phrase">{concat('string("',$newQt,'", mode="phrase", annotation_class="user")')}</stan:queryTerm>
        <stan:mode>RAW</stan:mode>
      </stan:query>
    else
      <stan:query>
        <stan:queryTerm type="phrase">{concat('string("',$newQt,'", mode="simpleany", annotation_class="user")')}</stan:queryTerm>
        <stan:mode>RAW</stan:mode>
      </stan:query>
  else
    <stan:query> 
      <stan:queryTerm>{$newQt}</stan:queryTerm>
      <stan:mode>AND</stan:mode>
    </stan:query>
return
  $query

output:

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm type="phrase">string("\"TEST\"", mode="phrase", annotation_class="user")</stan:queryTerm>
   <stan:mode>RAW</stan:mode>
</stan:query>

output (after changing $qt to let $qt := 'TEST'):

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm type="phrase">string("TEST", mode="simpleany", annotation_class="user")</stan:queryTerm>
   <stan:mode>RAW</stan:mode>
</stan:query>

output (after changing $qt to let $qt := '' which is what your if ($qt != "") then) would be catching):

<stan:query xmlns:stan="http://notsurewhatthisis.com/stan">
   <stan:queryTerm/>
   <stan:mode>AND</stan:mode>
</stan:query>

Hope this helps somehow.

冰火雁神 2024-10-26 05:33:52

要检查字符串是否包含双引号,请使用 fn:contains

fn:contains($string, '"')

您的代码区分三种情况,即

  • $qt 是空字符串、
  • $qt 包含引号、
  • $qt 不带引号。

我会这样写:

if ($qt = "") then
  <stan:query>
    <stan:queryTerm/>
    <stan:mode>AND</stan:mode>
  </stan:query>
else if (fn:contains($qt, '"'))then
  <stan:query>
    <stan:queryTerm>string("{fn:replace($qt, '"', '\\"')}", mode="phrase", annotation_class="user")</stan:queryTerm>
    <stan:mode>RAW</stan:mode>
  </stan:query>
else
  <stan:query>
    <stan:queryTerm>string("{$qt}", mode="simpleany", annotation_class="user")</stan:queryTerm>
    <stan:mode>RAW</stan:mode>
  </stan:query>

一些额外的注释:

  • 您可以将 $qt 或其衍生物嵌入其他元素内容中,因此无需事先使用 fn:concat 进行组装。
  • 构造 stan:mode 不需要嵌入表达式。
  • 替换字符串内的反斜杠必须由另一个反斜杠转义,但不得在其他任何地方这样做。

To check whether a string contains a double quote, use fn:contains:

fn:contains($string, '"')

Your code distinguishes three cases, namely

  • $qt is the empty string,
  • $qt containing quotes,
  • $qt without quotes.

I would write it like this:

if ($qt = "") then
  <stan:query>
    <stan:queryTerm/>
    <stan:mode>AND</stan:mode>
  </stan:query>
else if (fn:contains($qt, '"'))then
  <stan:query>
    <stan:queryTerm>string("{fn:replace($qt, '"', '\\"')}", mode="phrase", annotation_class="user")</stan:queryTerm>
    <stan:mode>RAW</stan:mode>
  </stan:query>
else
  <stan:query>
    <stan:queryTerm>string("{$qt}", mode="simpleany", annotation_class="user")</stan:queryTerm>
    <stan:mode>RAW</stan:mode>
  </stan:query>

Some extra remarks:

  • you can embed $qt or its derivative in other element content, so no need to assemble that with fn:concat beforehand.
  • no embedded expressions are needed for constructing stan:mode.
  • a backslash inside a replacement string must be escaped by another backslash, but this is must not be done anywhere else.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文