开发者

How to check in xquery if a string has double quote in it?

开发者 https://www.devze.com 2023-02-12 20:37 出处:网络
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 worki

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>


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号