ritzdiamond
ritzdiamond

Reputation: 269

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

I need to create a DOT graph on the basis of the following XML file.

<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>

For this purpose, I use the following query to produce the DOT markup for each layout-root element in the XML:

declare variable $newline := '&#10;';
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-leaf
let $graphics := $doc/realization//graphics
where $graphics[contains(@xref, $layout-leafs/@xref)]
return concat('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)
};

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

The result of the query for both layout-root elements is shown below:

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";
}

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.

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('"', $graphics/@xref, '" ', $dotgraphics, ';', $newline)
};

However, this does not produce the wanted outcome, because it returns the line "lay-1.03" [shape="box", style="filled", color="#b3c6ed"]; 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:

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";
}

Upvotes: 0

Views: 102

Answers (1)

Leo W&#246;rteler
Leo W&#246;rteler

Reputation: 4241

You need the realization, which is not in the sub-tree of $root. Try passing in just the graphics node:

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

let $doc := doc("layout-data.xml")/layout,
    $graphics := $doc/realization//graphics
for $root in $doc/layout-structure/*
return string-join(('graph "', $root/@id, '" { ', $newline,
         local:gfx($root, $graphics),local:ref($root),'}', $newline), "")

Upvotes: 1

Related Questions