MarkLogic联接查询 [英] MarkLogic Join Query

查看:67
本文介绍了MarkLogic联接查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是marklogic和Xquery世界的新手.我无法想到在Marklogic Xquery中编写以下逻辑的起点.如果有人可以给我想法/示例,以便实现以下目标,我将不胜感激:

Hi I am new to marklogic and in Xquery world. I am not able to think of starting point to write the following logic in Marklogic Xquery. I would be thankful if somebody can give me idea/sample so I can achieve the following:

我想基于B.XML中的单词查询来查询A.XML.查询应产生C.XML.逻辑应如下:

I want to Query A.XML based on a word lookup in B.XML. Query should produce C.XML. The logic should be as follows:

A.XML

<root>
<content> The state passed its first ban on using a handheld cellphone while driving in 2004 Nokia Vodafone Nokia Growth Recession Creicket HBO</content>
</root>

B.XML

<WordLookUp>
<companies>
    <company name="Vodafone">Vodafone</company>
    <company name="Nokia">Nokia</company>
</companies>
<topics>
    <topic group="Sports">Cricket</topic>
    <topic group="Entertainment">HBO</topic>
    <topic group="Finance">GDP</topic>
</topics>
<moods>
    <mood number="4">Growth</mood>
    <mood number="-5">Depression</mood>
    <mood number="-3">Recession</mood>
</moods>

C.XML(结果XML)

C.XML (Result XML)

<root>
    <content> The state passed its first ban on using a handheld cellphone while driving in 2004 Nokia Vodafone Nokia Growth Recession Creicket HBO</content>
    <updatedElement>
        <companies>
            <company count="1">Vodafone</company>
            <company count="2">Nokia</company>
        </companies>
        <mood>1</mood>
        <topics>
             <topic count="1">Sports</topic>
             <topic count="1">Entertainment</topic>
        </topics>
            <word-count>22</word-count>
    </updatedElement>
    </root>

  1. 在B.xml中搜索A.xml的每个公司/文本(),如果找到匹配项,则创建标记: TAG {company count =该单词的出现次数"}} company/@ name {/company}

  1. Search each company/text() of A.xml in B.xml, if match found create tag: TAG {company count="Number of occurrence of that word"}company/@name {/company}

在B.xml中搜索A.xml的每个主题/文本(如果找到匹配项,则创建标记) TAG {topic topic =该单词的出现次数"} topic/@ group {/topic}

Search each topic/text() of A.xml in B.xml, if match found create tag TAG {topic topic="Number of occurrences of that word"}topic/@group{/topic}

如果找到匹配项,则在B.xml中搜索A.xml的每个心情/文本() [第一个单词的出现* {/mood [第一个单词]/@ number}] + [第二个单词的出现* {/mood [第二个单词]/@ number})] ....

Search each mood/text() of A.xml in B.xml, if match found [occurrences of first word * {/mood[first word]/@number}] + [occurrences of second word * {/mood[second word]/@number})]....

获取元素的字数.

推荐答案

这是更简单/更简短且完全兼容的XQuery,不包含任何实现扩展,因此可以与任何兼容的XQuery 1.0处理器一起使用:

let $content := doc('file:///c:/temp/delete/A.xml')/*/*,
      $lookup := doc('file:///c:/temp/delete/B.xml')/*,
      $words := tokenize($content, '\W+')[.]
         return
           <root>
            {$content}
             <updatedElement>
               <companies>
                  {for $c in $lookup/companies/*,
                       $occurs in count(index-of($words, $c))
                     return
                       if($occurs)
                          then
                            <company count="{$occurs}">
                              {$c/text()}
                            </company>
                          else ()
                  }
               </companies>
               <mood>
                  {
                   sum($lookup/moods/*[false or index-of($words, data(.))]/@number)
                  }
               </mood>
               <topics>
                 {for $t in $lookup/topics/*,
                      $occurs in count(index-of($words, $t))
                    return
                      if($occurs)
                         then
                           <topic count="{$occurs}">
                             {data($t/@group)}
                           </topic>
                         else ()
                  }
               </topics>
               <word-count>{count($words)}</word-count>
              </updatedElement>
          </root>

应用于提供的文件A.xml和B.XML(包含在本地目录c:/temp/delete中)时,会产生所需的正确结果:

When applied on the provided files A.xml and B.XML (contained in the local directory c:/temp/delete), the wanted, correct result is produced:

<root>
   <content> The state passed its first ban on using a handheld cellphone while driving in 2004 Nokia Vodafone Nokia Growth Recession Cricket HBO</content>
   <updatedElement>
      <companies>
         <company count="1">Vodafone</company>
         <company count="2">Nokia</company>
      </companies>
      <mood>1</mood>
      <topics>
         <topic count="1">Sports</topic>
         <topic count="1">Entertainment</topic>
      </topics>
      <word-count>22</word-count>
   </updatedElement>
</root>

这篇关于MarkLogic联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆