I need to change my xml to group records type 20 as children of their previous record type 10:
<10>
<20>
<20>
</10>
<10>
<20>
</10>
…
My xQuery development is getting all records type 20 together per every record 10. Could you please help me to understand why? I need to group the records type 20 with is records type 10.
This is my actual development
let $doc := doc('converter:file:///\\Server-hps\tecnico\Proyectos\Caja Madrid\Xpresso\Stylus\Desarrollos\SICAV\XQUERY\SICAVXQUERY.conv?file:///\\Server-hps\tecnico\Proyectos\Caja Madrid\Xpresso\Stylus\Desarrollos\SICAV\XQUERY\SICAVXQUERYReducido.txt')
return
<root>
{
for $REG_10 in $doc/root/REG_10
return
<REG_10>
{$REG_10}
{
for $REG_20 in $doc/root/REG_20
Subject:Grouping in xQurey (Nested For command) Author:Minollo I. Date:18 Dec 2007 11:42 AM
The easiest way to do this is probably to leverage the "<<" and ">>" XPath operators; you can change your code into something like the following (note the "where" condition on the second "for"):
let $doc := doc(...)
return
<root>
{
for $REG_10 at $REG_10-startPos in $doc/root/REG_10
return
<REG_10>
{$REG_10}
{
for $REG_20 at $REG_20-pos in $doc/root/REG_20
where $REG_20 >> $REG_10 and (empty($REG_10/following-sibling::REG_10) or ($REG_20 << $REG_10/following-sibling::REG_10[1]))
return
<Datos>
{$REG_20}
</Datos>
}
</REG_10>
}
Subject:Grouping in xQurey (Nested For command) Author:Julio de la Vega Date:18 Dec 2007 12:00 PM
Thank you, it works and does what I need.
My problem now is to undertand what you have done because I have to apply it in other parts of my development. Where can I find information about the commands you have used? Could you please 'tanslate' it and give me an overview?
Subject:Grouping in xQurey (Nested For command) Author:Minollo I. Date:18 Dec 2007 12:32 PM
Read the "where" condition:
where $REG_20 >> $REG_10 and (empty($REG_10/following-sibling::REG_10) or ($REG_20 << $REG_10/following-sibling::REG_10[1]))
It's saying: consider only those $REG_20's that follow $REG_10 in document order (the ">>" operator - http://www.w3.org/TR/xpath20/#id-node-comparisons), and that appear before (the "<<" operator) in document order relative to the next REG_10 node (retrieved using the followin-sibling axis - http://www.w3.org/TR/xpath20/#axes); the empty(...) expression only checks the special case when there are no further REG_10 elements, at the end of the document.
Subject:Grouping in xQuery (Nested For command) Author:Julio de la Vega Date:19 Dec 2007 07:10 AM
Hi again,
According to the same issue, now I need to do a secong grouping inside the first grouping. I need to group the records type 20 acording one of its children (the criteria for secod grouping is one of the children of 20)