2012-09-04 30 views
1

我有以下XML文件:XQuery的標記化的字符串與單個和多個值

<layout> 
<layout-structure> 
    <layout-root id="layout-root"> 
    <layout-chunk id="header-text"> 
     <layout-leaf xref="lay-1.01"/> 
     <layout-leaf xref="lay-1.02"/> 
    </layout-chunk> 
    <layout-leaf xref="lay-1.03"/> 
    </layout-root> 
    <layout-root id="layout-root-two"> 
    <layout-chunk id="header-text-two"> 
    <layout-leaf xref="lay-1.04"/> 
    <layout-leaf xref="lay-1.05"/> 
    <layout-leaf xref="lay-1.06"/> 
    </layout-chunk> 
    <layout-leaf xref="lay-1.07"/> 
</layout-root> 
</layout-structure> 

<realization> 
    <text xref="lay-1.01 lay-1.04"/> 
    <text xref="lay-1.02 lay-1.05"/> 
    <graphics xref="lay-1.03 lay-1.06" type="1"/> 
    <graphics xref="lay-1.07" type="2"/> 
</realization> 
</layout> 

我要提取的值的圖形元件的外部參照屬性,以便限制輸出在中爲子句的功能如下所示。

declare function local:gfx($root, $graphics) { 
let $graphic-xrefs := tokenize($graphics/@xref, " ") 
for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs] 
return concat('"', $layout-leafs/@xref, '" ', $dotgraphics, ';', $newline) 
}; 

然而,這會導致錯誤,因爲下圖形一些外部參照的屬性元件包含一個單一的值,如在<graphics xref="lay-1.07"/>的情況。

是否有可能使用標記化來獲取圖形/外部參照值,或者我應該使用不同的方法?

回答

1

你可以試着改變你創建$graphic-xrefs的方式......

declare function local:gfx($root, $graphics) { 
    let $graphic-xrefs := 
     for $xref in $graphics/@xref 
     return 
      tokenize($xref,' ') 
    for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs] 
    return concat('"', $layout-leafs/@xref, '" ', $dotgraphics, ';', $newline) 
}; 
0

這應該不會引起問題,因爲如果拆分字符串不在搜索字符串中,tokenize將簡單地返回整個字符串。

對於處理這樣的一般情況下,正常的if (...) then ... else ...聲明可能是有用的。此外,您可能需要使用try { ... } catch {...}構造來處理意外情況。

您的代碼實際上是按預期工作,當我運行此:

declare variable $t := 
<layout> 
<layout-structure> 
    <layout-root id="layout-root"> 
    <layout-chunk id="header-text"> 
     <layout-leaf xref="lay-1.01"/> 
     <layout-leaf xref="lay-1.02"/> 
    </layout-chunk> 
    <layout-leaf xref="lay-1.03"/> 
    </layout-root> 
    <layout-root id="layout-root-two"> 
    <layout-chunk id="header-text-two"> 
    <layout-leaf xref="lay-1.04"/> 
    <layout-leaf xref="lay-1.05"/> 
    <layout-leaf xref="lay-1.06"/> 
    </layout-chunk> 
    <layout-leaf xref="lay-1.07"/> 
</layout-root> 
</layout-structure> 

<realization> 
    <text xref="lay-1.01 lay-1.04"/> 
    <text xref="lay-1.02 lay-1.05"/> 
    <graphics xref="lay-1.03 lay-1.06" type="1"/> 
    <graphics xref="lay-1.07" type="2"/> 
</realization> 
</layout>; 

declare function local:gfx($root, $graphics) { 
let $graphic-xrefs := tokenize($graphics/@xref, " ") 
for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs] 
return $layout-leafs/@xref 
}; 

local:gfx($t, $t/realization/graphics[2]) 

請注意,在你的代碼片段中最後<layout>實際上應該是一個致閉幕詞。

+0

感謝您的輸入@dirkk!奇怪的是,我在Editix中得到以下錯誤信息:'不允許多個項目的序列作爲tokenize()的第一個參數(「lay-1.03」,「lay-1.06」)' – ritzdiamond

+0

發生了這種事情當你真的執行我的代碼(或只是爲了你的原始代碼)?因爲對於我的代碼,我會感到很驚訝。該錯誤消息還表明,確實錯誤在於如何調用函數'local:gfx':聽起來像$ graphics/@ xref不僅僅是一個單獨的元素,而是一系列多個節點。但是作爲tokenize的輸入,你必須傳遞一個xs:string並且沒有序列。我強烈建議檢查你如何調用函數和你傳遞的變量。如果您想要進一步提供建議,請張貼您稱呼該功能的方式。 – dirkk