我所做的是创建了一个 XML 文件,其中包含我需要在文档上执行的数千个搜索词的列表。然后,我从一组搜索词样本中创建了这个查询,作为测试,以针对测试文档执行,其中包含来自实际文档的一些样本:
let $keywords := ("best clients", "Very", "20")
for $keyword in $keywords
let $matches := doc('test')/set/entry[matches(comment, $keyword, 'i')]
return (<re>
{subsequence($matches/comment, 1, 1),
subsequence($matches/buyer, 1, 1)}</re>,
<re>
{subsequence($matches/comment, 2, 1),
subsequence($matches/buyer, 2, 1)}
</re>
)
试图回来<re><comment /><buyer /></re><re><comment /><buyer /></re>... continuous
,但我以粗略的顺序让他们回来。
这是正在解析的文档中的一个块(我已经删除了买家姓名和一些嵌套,以使其更易于阅读):
<set>
<entry>
<comment>The client is only 20 years old. Do not be surprised by his youth.</comment>
<buyer></buyer>
<id>1282</id>
<industry>International Trade; Fish and Game</industry>
</entry>
<entry>
<comment>!On leave in October.</comment>
<buyer></buyer>
<id>709</id>
<industry>Real Estate</industry>
</entry>
<entry>
<comment>Is often !out between 1 and 3 p.m.</comment>
<buyer></buyer>
<id>127</id>
<industry>Virus Software Marketting</industry>
</entry>
<entry>
<comment>Very personable. One of our best clients.</comment>
<buyer></buyer>
<id>14851</id>
<industry>Administrative support.</industry>
</entry>
<entry>
<comment>!Very difficult to reach, but one of our top buyers.</comment>
<buyer></buyer>
<id>1458</id>
<industry>Construction</industry>
</entry>
<entry>
<comment></comment>
<buyer></buyer>
<id>276470</id>
<industry>Bulk Furniture Sales</industry>
</entry>
<entry>
<comment>A bit of an eccentric. One of our best clients.</comment>
<buyer></buyer>
<id>1506</id>
<industry>Sports Analysis</industry>
</entry>
<entry>
<comment>Very gullible, so please !be sure she needs what you sell her. She's one of our best clients.</comment>
<buyer></buyer>
<id>1523</id>
<industry>International Trade</industry>
</entry>
<entry>
<comment>He wants to buy everything, but !he has a tight budget.</comment>
<buyer></buyer>
<id>1524</id>
<industry>Public Relations</industry>
</entry>
</set>
我使用的一些关键字:“最佳客户*”、“贸易”、“20”、....
我一直
输出是一长串条目,在 entry 元素下有评论和买家子代作为兄弟姐妹。我想限制返回的条目数2 per keyword
。我还试图让以感叹号(!)开头的评论作为优先事项。
当前输出(接近):
<re><comment>Very personable. One of our best clients.</comment>
<buyer/>
</re><re><comment>A bit of an eccentric. One of our best clients.</comment>
<buyer/>
</re><re><comment>Very personable. One of our best clients.</comment>
<buyer/>
</re><re><comment>!Very difficult to reach, but one of our top buyers.</comment>
<buyer/>
</re><re><comment>The client is only 20 years old. Do not be surprised by his youth.</comment>
<buyer/>
</re><re/>
当前输出格式:
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment> //Not prioritized.
<buyer></buyer>
</entry>
<entry>
<comment>keywordc</comment>
<buyer></buyer>
</entry>
期望的输出:
<entry>
<comment>!keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>keyworda</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>
<entry>
<comment>!keywordb</comment>
<buyer></buyer>
</entry>
(基本上,优先考虑包含感叹号的条目并将结果限制为每个关键字 2 个。)。