3

BaseXXMLデータベースを使用しています。次のようなデータベース内のxmlドキュメントについて考えてみます。

<entries>
<book-entry>
  <book>Book 1</book>
  <author>Author 1 ABC</author>
  <title>Title 1</title>
</book-entry>
<car-entry>
  <car>Car 1</car>
  <model>Model 1</model>
  <price>Price 1 ABC</price>
</car-entry>
</entries>

私は、次のようなさまざまなオプションを使用して検索を実行しようとしています。本のみ、車のみ、本と車の両方を検索します。

xqueryでxml変数を使用して、必要な検索タイプに基づいて検索結果を返そうとしています。

変数値の例:- <types><type>book-entry</type></types>:book-entriesのみを<types><type>car-entry</type></types>検索-:car-entriesのみを<types><type>book-entry</type><type>car-entry</type></types>検索-:book-entriesとcar-entriesを検索

XQueryサンプル:

declare variable $doc_name as xs:string external; (: name of xml document :)
declare variable $search_types as xs:anyAtomicType external; (: one of the example variable values shown above :)
declare variable $search_key as xs:string external; (: eg: ABC :)

for $entry in doc($doc_name)/entries[*[exists($search_types/types/type/text() = node-name(.)) and .//text() contains text $search_key]]
  return $entry

上記のクエリは、<types><type>car-entry</type></types>$ search_typesに渡しますが、テキストの子ノードABCを含む車と本の両方のエントリを返します。

xml変数を使用して検索を制限するにはどうすればよいですか?これを行うためのより良い方法はありますか?また、xml変数に両方のタイプの子ノードがある場合、xqueryは車とエントリの両方を返す必要があります。

ありがとう、ソニー

4

2 に答える 2

2
for $entry in doc($doc_name)/entries
         [*[exists($search_types/types/type/text() = node-name(.)) 
        and 
          .//text() contains text $search_key
           ]
         ]  return $entry

する必要があります

for $entry in doc($doc_name)/entries/*
        [exists($search_types/types/type/text() = node-name(.)) 
       and 
         .//text() contains text $search_key]  
  return $entry

または、代わりに、この単純なXPath式を使用することもできます

/*/*[name() eq $vSearchTypes/types/type
   and
     .//text()[contains(., $vSearchKey)]
    ]

最後に、このXQuery式

let $vSearchTypes :=
  <types>
    <type>book-entry</type>
</types>,

$vSearchKey := 'ABC'

return
  /*/*[name(.) eq $vSearchTypes/type
        and
          .//text()[contains(., $vSearchKey)]
         ]

提供されたXMLドキュメントに適用した場合:

<entries>
  <book-entry>
    <book>Book 1</book>
    <author>Author 1 ABC</author>
    <title>Title 1</title>
  </book-entry>
  <car-entry>
    <car>Car 1</car>
    <model>Model 1</model>
    <price>Price 1 ABC</price>
  </car-entry>
</entries>

必要な正しい結果を生成します

<book-entry>
    <book>Book 1</book>
    <author>Author 1 ABC</author>
    <title>Title 1</title>
  </book-entry>
于 2011-05-22T19:01:16.250 に答える
1

質問1-uはfn:data()を使用して、Xml固有のものからすべてのユーザー入力値をエスケープしようとする可能性があります。

質問2-Xmlデータベースを使用している場合は、Xpathよりもデータベース検索APIを活用してみてください。そうでない場合は、xpathを直接追跡するのではなく、XPathグラマーを形成できる定義済みの抽象xquery検索レイヤーを考え出す必要があります。

于 2011-05-22T13:17:26.340 に答える