跳轉到內容

XQuery/快取和索引

來自華夏公益教科書,開放的書籍,開放的世界

需要使用索引補充有關各個團隊或組的資料檢視,這些索引適合這些檢視的資源。按需生成索引是一種方法,但會載入 SPARQL 伺服器。鑑於 DBpedia 提取的批處理性質,將索引資料快取並使用快取生成索引頁面更有意義。(觸發快取重新整理是另一個問題!)

非快取方法

[編輯 | 編輯原始碼]

以下指令碼生成一個索引頁面,其中包含指向藝術家專輯的 HTML 檢視和時間線檢視的連結。

declare option exist:serialize "method=xhtml media-type=text/html";
declare variable $query := "
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX p: <http://dbpedia.org/property/> 
SELECT * WHERE { 
      ?group  skos:subject <http://dbpedia.org/resource/Category:Rock_and_Roll_Hall_of_Fame_inductees>.
   }
 ";

declare function local:clean($text) {
    let $text:= util:unescape-uri($text,"UTF-8")
    let $text := replace($text,"\(.*\)","")
    let $text := replace($text,"_"," ")
    return $text
};

 let $category := request:get-parameter("category","")
 let $categoryx := replace($category,"_"," ")
 let $queryx := replace($query,"Rock_and_Roll_Hall_of_Fame_inductees",$category)
 let $sparql := concat("http://dbpedia.org/sparql?default-graph-uri=",escape-uri("http://dbpedia.org",true()),
                                      "&query=",escape-uri($queryx,true())
                         )
let $result  := doc($sparql)
return
<html>
<body>
  <h1>{$categoryx}</h1>
  <table border="1">
   {  for $row in $result/table//tr[position()>1]
      let $resource := substring-after($row/td[1],"resource/")
      let $name := local:clean($resource)
      order by $name
      return      
        <tr>
           <td>         
           {$name}
           </td>
           <td>
           <a href="group2html.xq?group={$resource}">HTML</a>
           </td>
           <td>
           <a href="groupTimeline.xq?group={$resource}">Timeline</a>
           </td>
           </tr>
   }
   </table>
 </body>
 </html>

索引示例

[編輯 | 編輯原始碼]

快取方法

[編輯 | 編輯原始碼]

需要兩個指令碼 - 一個用於生成要快取的資料,另一個用於生成索引頁面。該方法以一個基於維基百科類別搖滾名人堂入選者的搖滾樂隊索引為例進行說明。

生成索引資料

[編輯 | 編輯原始碼]

此指令碼生成一個 XML 檔案。進一步的開發會將 XML 直接儲存到資料庫中,但也可以手動儲存到相應的位置。它由一個類別引數化。

declare variable $query := "
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX p: <http://dbpedia.org/property/> 
SELECT * WHERE { 
      ?group  skos:subject <http://dbpedia.org/resource/Category:Rock_and_Roll_Hall_of_Fame_inductees>.
   }
 ";

declare function local:clean($text) {
    let $text:= util:unescape-uri($text,"UTF-8")
    let $text := replace($text,"\(.*\)","")
    let $text := replace($text,"_"," ")
    return $text
};

declare function local:table-to-seq($table ) {
   let $head := $table/tr[1]
   for $row in $table/tr[position()>1]
   return
     <tuple>
            { for $cell at $i in $row/td
               return
                 element {$head/th[position()=$i]}
                     {string($cell)}
            }
      </tuple>
 };
 
let $category := request:get-parameter("category","Rock_and_Roll_Hall_of_Fame_inductees")
let $queryx := replace($query,"Rock_and_Roll_Hall_of_Fame_inductees",$category)
let $sparql := concat("http://dbpedia.org/sparql?default-graph-uri=",escape-uri("http://dbpedia.org",true()),
                                      "&query=",escape-uri($query,true())
                         )
let $result  := doc($sparql)/table
let $groups := local:table-to-seq($result)
return
<ResourceList category="{$category}">
   {for $group in $groups
    let $resource := substring-after($group/group,"resource/")
    let $name := local:clean($resource)
    order by $name
    return      
        <resource id="{$resource}" name="{$name}"/>
   }
</ResourceList>

注意:我想更好的方法是使用三元組,儲存到本地三元組儲存中。

HTML 索引頁面

[編輯 | 編輯原始碼]

此指令碼,groupList,使用快取的索引資料

declare option exist:serialize "method=xhtml media-type=text/html";
let $list := //ResourceList[@category="Rock_and_Roll_Hall_of_Fame_inductees"]
return
<html>
<body>
  <h1>Rock Groups</h1>
  <table border="1">
   {for $declare option exist:serialize "method=xhtml media-type=text/html";
lresource in $list/resource
    order by $resource/@name
   return      
        <tr>
           <td>         
           {string($resource/@name)}
           </td>
           <td>
           <a href="group2html.xq?group={$resource/@id}">HTML</a>
           </td>
           <td>
           <a href="groupTimeline.xq?group={$resource/@id}">Timeline</a>
           </td>
           </tr>
   }
   </table>
 </body>
 </html>


搖滾樂隊

華夏公益教科書