2011-10-07 18 views
1

現在我有一個DITA複合材料,如:如何做出正確的語法的XQuery環接

<root> 
<topic>....</topic> 
<topic>....</topic> 
<topic>....</topic> 
<topic>....</topic> 
<topic>....</topic> 
</root> 

而我只需要編寫基本上將爲每個主題一個ditamap的XQuery,所以resutling ditamap應該看像:

<map> 
<topicref>....</topicref> 
<topicref>....</topicref> 
<topicref>....</topicref> 
<topicref>....</topicref> 
<topicref>....</topicref> 
</map> 

我現在的XQuery是不太做正確的事,它能夠捕捉到每一個話題,但不是創建一個ditamp,它爲每個主題創建多個ditamap,其一:

$isFoSaved := for $b in $mapNode/*[local-name() = 'topic'] 
       let          
       $topicWithPI := let $holder:=1 
       return (
         <topicref href="1.xml#Begin" navtitle="Begin" scope="local" type="topic"/> 
       ), 

專家能幫忙嗎?謝謝

回答

1

我只能看到你嵌入了多個flwor表達式。

無論何時使用$x := let $y ...$x := for $y ...,都會啓動一個新的flwor表達式,該表達式必須由return子句關閉。因此,您剪下的代碼無效/不完整:您有兩個打開的flwor表達式,但只有一個return子句。

如果你試圖保持它平坦,它會容易得多。

例如:

<map>{ 
let $mapNode := 
    <root> 
    <topic>....</topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    </root> 
    for $b in $mapNode/*[local-name() = 'topic'] 
    return 
    <topicref href="1.xml#Begin" 
       avtitle="Begin" 
       scope="local" 
       type="topic"/> 
}</map> 

此查詢工作在try.zorba-xquery.com,但我不知道這是你在找什麼?

+0

謝謝。這真的很有幫助。我能再問你一些問題嗎?如果我們有嵌套的話題(即主題內的話題),我們是否能夠在xquery中捕獲這些話題? – Kevin

+0

當然,這就是XQuery/XPath的最佳選擇。如果您只是使用$ mapNode // * [local-name()='topic'](雙斜槓),您將獲得所有派生的topicrefs。如果您想保留層次結構,那麼它會更復雜一點:請參閱其他答案。 –

1

如果您想保留嵌套主題的層次結構,它會更復雜一點。我認爲這是最好用這個遞歸函數:

declare function local:topicref($topics) 
{ 
    for $b in $topics 
    return 
    <topicref href="1.xml#Begin" 
       avtitle="Begin" 
       scope="local" 
       type="topic">{ 
     local:topicref($b/*[local-name() = 'topic']) 
    }</topicref> 

}; 

<map>{ 
let $mapNode := 
    <root> 
    <topic><topic>....</topic></topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    <topic>....</topic> 
    </root> 
return 
    local:topicref(
    $mapNode/*[local-name() = 'topic'] 
    ) 
}</map> 

結果:

<?xml version="1.0" encoding="UTF-8"?> 
<map> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/> 
    </topicref> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/> 
    <topicref href="1.xml#Begin" avtitle="Begin" scope="local" type="topic"/> 
</map>