set search_path to xml; CREATE TABLE templates ( id integer NOT NULL, template xnt ); INSERT INTO templates VALUES -- the simplest case: only attribute values are parametrized (0, ''), -- in addition, parameter is used to generate the element name as well as attribute names (1, ' '), -- node can also be used as parameter (2, ' '), (3, ''); SELECT id, template FROM templates ORDER BY id; id | template ----+----------------------------------------------------------------------------------------------------- 0 | 1 | + | + | + | + | + | 2 | + | + | 3 | (4 rows) CREATE TABLE points ( id integer NOT NULL, x float NOT NULL, y float NOT NULL ); INSERT INTO points VALUES (0, 0.0, 0.0), (1, 0.3, 5.4), (2, 3.15, 0.01); CREATE TABLE point_nodes ( id integer NOT NULL, p node NOT NULL ); INSERT INTO point_nodes SELECT p.id, xml.node(t.template, '{p1, p2}', ROW (x, y)) FROM templates t, points p WHERE t.id=0; SELECT * FROM point_nodes ORDER BY id; id | p ----+---------------------------- 0 | 1 | 2 | (3 rows) SELECT xml.node(template, '{p1, p2, p3}', ROW (p.id, p.x, p.y)) FROM templates t, points p WHERE t.id=1 ORDER BY p.id; node ------------------------------------- (3 rows) WITH fragments(f) AS ( -- order the rows so that the fragment is never different from the expected test output. SELECT xml.fragment(p ORDER BY id) FROM point_nodes ) SELECT xml.node(t.template, '{p1}', ROW(f)) FROM fragments f, templates t WHERE t.id=2; node ------------------------------------------------------------------------------------- (1 row) -- Is unary minus processed correctly when used with expression parameters? select xml.node('', '{ x }', ROW( 3.14::float )); node ---------------- (1 row) select xml.node('', '{ x }', ROW( -3.14::float )); node ---------------- (1 row) select xml.node('', '{ x }', ROW( -3.14::float )); node --------------- (1 row) DROP TABLE templates; DROP TABLE points; DROP TABLE point_nodes;