How to use XQuery functions to parse a specific part of the source XML?

darkstorm

New Member
I need to create a DOT graph on the basis of the following XML file.\[code\]<layout> <layout-structure> <layout-root id="layout-root"> <layout-chunk id="header-text"> <layout-leaf xref="lay-1.01" location="row-1" area-ref="content"/> <layout-leaf xref="lay-1.02" location="row-2" area-ref="content"/> </layout-chunk> <layout-leaf xref="lay-1.03" location="row-3" area-ref="content"/> </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-chunk> </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"/></realization><layout>\[/code\]For this purpose, I use the following query to produce the DOT markup for each layout-root element in the XML:\[code\]declare variable $newline := '
';declare variable $dotgraphics := '[shape="box", style="filled", color="#b3c6ed"]';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)};declare function local:gfx($doc) {for $layout-leafs in $doc//layout-leaflet $graphics := $doc/realization//graphicswhere $graphics[contains(@xref, $layout-leafs/@xref)]return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)};let $doc := doc("layout-data.xml")/layoutfor $root in $doc/layout-structure/*return string-join(('graph "', $root/@id, '" { ', $newline,local:gfx($doc),local:ref($root),'}', $newline), "")\[/code\]The result of the query for both layout-root elements is shown below:\[code\]graph "layout-root" { "lay-1.03" [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";}graph "layout-root-two" { "lay-1.03" [shape="box", style="filled", color="#b3c6ed"];"layout-root-two" -- "header-text-two";"header-text-two" -- "lay-1.04";"header-text-two" -- "lay-1.05";}\[/code\]As you can see, the query uses the following function to change the colour and shape of the DOT markup, if an element is of the type graphics.\[code\]declare function local:gfx($doc) {for $layout-leafs in $doc//layout-leaflet $graphics := $doc/realization//graphicswhere $graphics[contains(@xref, $layout-leafs/@xref)]return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)};\[/code\]However, this does not produce the wanted outcome, because it returns the line \[code\]"lay-1.03" [shape="box", style="filled", color="#b3c6ed"];\[/code\] also for the second graph (named layout-root-two), although it should only appear in the first graph (named layout-root).How should I modify the function, so that it checks for the graphics elements in case of each layout-root under layout-structure?I have attempted to call the function using the variable $root, but this results in a static error.The wanted outcome is displayed below:\[code\]graph "layout-root" { "lay-1.03" [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";}graph "layout-root-two" { "layout-root-two" -- "header-text-two";"header-text-two" -- "lay-1.04";"header-text-two" -- "lay-1.05";}\[/code\]
 
Top