2012-09-03 56 views
1

我需要從以下XML中生成DOT圖。在XQuery中調用concat函數中的多個函數

<layout> 
<layout-structure> 
    <layout-root id="layout-root" orientation="landscape"> 
     <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-leaf xref="lay-1.03"/> 
    </layout-root> 
</layout-structure> 
<realization> 
    <text xref="lay-1.01"/> 
    <text xref="lay-1.02"/> 
    <graphics xref="lay-1.03 lay-1.04"/> 
</realization> 
</layout> 

我用下面的XQuery來生成DOT標記:

declare variable $newline := '&#10;'; 

declare function local:ref($root) { 
    string-join((
    for $chunk in $root/layout-chunk 
    return (
     concat(' "', $root/@id, '" -- "', $chunk/@id, '";', $newline), 
    local:ref($chunk) 
), 
local:leaf($root)), "") 
}; 

declare function local:leaf($root) { 
for $leaf in $root/layout-leaf 
return concat(' "', $root/@id, '" -- "', $leaf/@xref, '";', $newline) 
}; 

let $doc := doc("layout-data.xml")/layout 
let $root := $doc/layout-structure/* 
return concat('graph "', $root/@id, '" { ', $newline, local:ref($root),'}') 

上面的查詢工作正常,並且產生下面的圖:

graph "layout-root" { 
"layout-root" -- "header-text"; 
"header-text" -- "lay-1.01"; 
"header-text" -- "lay-1.02"; 
"layout-root" -- "lay-1.03"; 
"layout-root" -- "lay-1.04"; 
} 

的結果如下所示:

現在,我想要做的是指定一組用於在點圖中的每個元素根據它們的屬性,在XML中實現元素定義,屬性如下圖所示:

這,當然,需要以下DOT標記:

graph "layout-root" { 
"lay-1.03" [shape="box", style="filled", color="#b3c6ed"]; 
"lay-1.04" [shape="box", style="filled", color="#b3c6ed"]; 
"layout-root" -- "header-text"; 
"header-text" -- "lay-1.01"; 
"header-text" -- "lay-1.02"; 
"layout-root" -- "lay-1.03"; 
"layout-root" -- "lay-1.04"; 
} 

我已經寫了兩個額外的變量和函數來選擇和寫入所需的DOT標記:

declare variable $dotgraphics := '[shape="box", style="filled", color="#b3c6ed"]'; 

declare function local:gfx($doc) { 
for $layout-leafs in $doc//layout-leaf 
let $graphics := $doc/realization//graphics 
where $graphics[contains(@xref, $layout-leafs/@xref)] 
return concat($layout-leafs/@xref, ' ', $dotgraphics, ';', $newline) 
}; 

我的問題是:我怎麼包括功能地方:GFX上述工作的XQuery腳本?

如果我只需調用功能*本地:GFX之前本地($ DOC):REF($根)所示爲,

return concat('graph "', $root/@id, '" { ', $newline, local:gfx($doc), $newline, local:ref($root),'}') 

查詢返回一個錯誤,多個項目的序列不能作爲concat函數的參數;這怎麼解決?

回答

1

對此,您可以使用fn:string-join($strings[, $separator]),而不是將字符串作爲序列。如果您使用$newline作爲第二個參數(默認爲空字符串),它甚至插入換行符爲您提供:

string-join(('foo', 'bar', 'baz'), '&#10;') 

產量

foo 
bar 
baz 
+0

感謝@ LEO-worteler;我曾嘗試過串連接,但不知何故設法弄亂了語法。 只是總結一下,正確的結果是: 'return string-join((''''',$ root/@ id,'「{',$ newline,local:gfx($ doc), local:ref($ root),'}'),「」)' – ritzdiamond