我是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>
答案 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, '"')
您的代码区分了三种情况,即
我会这样写:
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>
一些额外的评论: