I have a database like this:
<data> <templates> <template> <id>123</id> <content> <html> <head><title>My Template</title></head> <body><text/></body> </html> </content> </template> </templates> <pages> <page> <id>456</id> <content> <p>This is some text.</p> </content> </page> </pages> </data>
I want to write an XQuery that results in this:
<html> <head><title>My Template</title></head> <body><p>This is some text.</p></body> </html>
So <text/> is replaced by <p>This is some text.</p>
Here is the start of an XQuery select:
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return $x
That gets me the template. How do I insert $y in place of $x/body/text ? I've searched all over with no luck. I can do this in the business logic, but so much better to do it in the query.
Also, I'd like to be able to do this recursively. Is that possible? Do I have to write my own function for this?
Thanks much for any help!
Chas.
Good Morning Charles,
this sounds like a case for the 'transform' expression which is part of XQuery Update.
The transform expression modifies a copy of nodes without touching the original nodes / making permanent changes to your database. I'm sorry for our documentation not being clear about this. We are working on that.
Taking your query as a start ...
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return $x
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return ( copy $c := $x modify replace node $c/body/text with $y return $c )
... should do the trick.
Also, I'd like to be able to do this recursively. Is that possible? Do I have to write my own function for this?
Sorry - I'm not sure what you mean here. But if you provide me with further details (i.e. a larger document snippet or more details about your use case) I'd be happy to help you with that.
Cheers, Lukas
This works perfectly. Thanks much! It's true that things are a bit hard to find on the site, but I understand what a huge undertaking it is to keep up documentation. I appreciate your help.
Re recursion, I am building a CMS based on BaseX, Circumflex (Scala), and Vaadin. The example below was greatly simplified. In the actual DB, the <page> would look something like this:
<page> <id>456</id> <name>Home</name> <title></title> <path>/</path> <segments> <segment id="abc" position="0"/> <segment id="def" position="1"/> <segment id="ghi" position="2"/> </segments> </page>
Then:
<segments> <segment> <id>abc</id> <name>Home</name> <columns> <column id="vvv" position="0"/> </columns> </segment> <segment> <id>def</id> <name>Home</name> <columns> <column id="www" position="0"/> <column id="xxx" position="1"/> <column id="yyy" position="2"/> </columns> </segment> <segment> <id>ghi</id> <name>Home</name> <columns> <column id="zzz" position="0"/> </columns> </segment> </segments>
Then:
<columns> <column> <id>vvv</id> <name>Page Header</name> <items> <item id="666" position="1"/> </items> </column> </columns>
And so on. The idea is to write a single query and optimize it so that it will recursively pull items from a list of items and pop their content into the appropriate columns, which will be popped into the page segments, which will be assembled into the final page.
Clever ideas for doing this greatly appreciated.
Chas.
On 12/01/2010 4:50 AM, Lukas Kircher wrote:
Good Morning Charles,
this sounds like a case for the 'transform' expression which is part of XQuery Update.
The transform expression modifies a copy of nodes without touching the original nodes / making permanent changes to your database. I'm sorry for our documentation not being clear about this. We are working on that.
Taking your query as a start ...
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return $x
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return ( *copy $c := $x* ***modify replace node $c/body/text with $y* ***return $c* ) / / ... should do the trick.
Also, I'd like to be able to do this recursively. Is that possible? Do I have to write my own function for this?
Sorry - I'm not sure what you mean here. But if you provide me with further details (i.e. a larger document snippet or more details about your use case) I'd be happy to help you with that.
Cheers, Lukas
Hi Charles,
I'm not yet sure if I get it right - sorry. Based on your last mail I assume you want to carry this out bottom-to-top (item->column-
segment), am I right? Or is it the other way round? The number of
recursion steps is limited, right (page...item)? So it's not actually recursion we need to solve this?
Cheers, Lukas
On Dec 1, 2010, at 9:19 AM, Charles F. Munat wrote:
This works perfectly. Thanks much! It's true that things are a bit hard to find on the site, but I understand what a huge undertaking it is to keep up documentation. I appreciate your help.
Re recursion, I am building a CMS based on BaseX, Circumflex (Scala), and Vaadin. The example below was greatly simplified. In the actual DB, the <page> would look something like this:
<page> <id>456</id> <name>Home</name> <title></title> <path>/</path> <segments> <segment id="abc" position="0"/> <segment id="def" position="1"/> <segment id="ghi" position="2"/> </segments> </page>
Then:
<segments> <segment> <id>abc</id> <name>Home</name> <columns> <column id="vvv" position="0"/> </columns> </segment> <segment> <id>def</id> <name>Home</name> <columns> <column id="www" position="0"/> <column id="xxx" position="1"/> <column id="yyy" position="2"/> </columns> </segment> <segment> <id>ghi</id> <name>Home</name> <columns> <column id="zzz" position="0"/> </columns> </segment> </segments>
Then:
<columns> <column> <id>vvv</id> <name>Page Header</name> <items> <item id="666" position="1"/> </items> </column> </columns>
And so on. The idea is to write a single query and optimize it so that it will recursively pull items from a list of items and pop their content into the appropriate columns, which will be popped into the page segments, which will be assembled into the final page.
Clever ideas for doing this greatly appreciated.
Chas.
On 12/01/2010 4:50 AM, Lukas Kircher wrote:
Good Morning Charles,
this sounds like a case for the 'transform' expression which is part of XQuery Update.
The transform expression modifies a copy of nodes without touching the original nodes / making permanent changes to your database. I'm sorry for our documentation not being clear about this. We are working on that.
Taking your query as a start ...
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return $x
for $x in /data/templates/template[id='123']/content/* for $y in /data/pages/page[id='456']/content/* return ( *copy $c := $x* ***modify replace node $c/body/text with $y* ***return $c* ) / / ... should do the trick.
Also, I'd like to be able to do this recursively. Is that possible? Do I have to write my own function for this?
Sorry - I'm not sure what you mean here. But if you provide me with further details (i.e. a larger document snippet or more details about your use case) I'd be happy to help you with that.
Cheers, Lukas
basex-talk@mailman.uni-konstanz.de