如果字符串中有双引号,如何检入xquery?

时间:2011-02-24 00:38:39

标签: string xquery

我是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>

2 个答案:

答案 0 :(得分:0)

这个问题很难回答,因为我无法复制您的问题(我根本无法轻松运行您的代码)。我也没有使用过“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>

输出(将$qt更改为let $qt := '',这是if ($qt != "") then)将要捕获的内容:

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

希望这会有所帮助。

答案 1 :(得分:0)

要检查字符串是否包含双引号,请使用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时不需要嵌入表达式。
  • 替换字符串中的反斜杠必须通过另一个反斜杠转义,但不能在其他任何地方进行。