Subject:Looking for xQuery help Author:Minollo I. Date:18 Sep 2008 01:32 PM
Something like this:
declare option ddtek:serialize "indent=yes";
declare function local:getFields($item) {
let $nextItem := $item/following-sibling::*[local-name()!="FieldAndValuesPairs"][1]
for $field in $item/following-sibling::*[local-name()="FieldAndValuesPairs"]
where if($nextItem) then $field << $nextItem else true()
return $field
};
<Documents> {
for $Template in /root/Template
return
<Document>
<TemplateName>{$Template/TemplateName/text()}</TemplateName>
{
for $field in local:getFields($Template)
return
element {$field/*/local-name()} {$field/*/text()}
}
</Document>
} </Documents>
Subject:Looking for xQuery help Author:sebastien vinchon Date:18 Sep 2008 08:19 PM
Thank you for the feedback but I had already been able to obtain such a result using the code below.
My problem is actually to be able to nest the "Document" nodes whose template name does not start with "MPOL" as "SubDocument" nodes below the preceding MPOL.
This is shown by the "ExpectResult" XML I provided initially if you look at it in details
Here is my current xquery code (that does not do the needed nesting):
declare option ddtek:serialize "indent=yes";
declare function local:getRelatedRecords($item) {
let $nextItem := $item/following-sibling::*[local-name()="Template"][1]
for $related in $item/following-sibling::*[local-name()!="Template"]
where if($nextItem) then $related << $nextItem else true()
return $related
};
<Documents>
{
for $Template in /root/Template
return
<Document>
{$Template/*}
{
for $NOT_Template in local:getRelatedRecords($Template)
return
$NOT_Template/*
}
</Document>
}
</Documents>
Subject:Looking for xQuery help Author:sebastien vinchon Date:18 Sep 2008 08:21 PM
If you look at "POL_Modified_xQueryExpectedResult.xml", you'll notice that it contains only 2 "Document" nodes (and not 8) and that each ot them has 3 "SubDocument" nodes.
Subject:Looking for xQuery help Author:Minollo I. Date:18 Sep 2008 09:04 PM
Same concept:
declare option ddtek:serialize "indent=yes";
declare function local:getSubDocuments($item) {
let $nextItem := $item/following-sibling::*[local-name()="Template" and starts-with(TemplateName,"MPOL")][1]
for $field in $item/following-sibling::*[local-name()="Template"]
where if($nextItem) then $field << $nextItem else true()
return $field
};
declare function local:getFields($item) {
let $nextItem := $item/following-sibling::*[local-name()!="FieldAndValuesPairs"][1]
for $field in $item/following-sibling::*[local-name()="FieldAndValuesPairs"]
where if($nextItem) then $field << $nextItem else true()
return $field
};
<Documents> {
for $Template in /root/Template[starts-with(TemplateName,"MPOL")]
return
<Document>
<TemplateName>{$Template/TemplateName/text()}</TemplateName>
{
for $field in local:getFields($Template)
return
element {$field/*/local-name()} {$field/*/text()},
for $subDocument in local:getSubDocuments($Template)
return
<SubDocument>
<TemplateName>{$subDocument/TemplateName/text()}</TemplateName>
{
for $field in local:getFields($subDocument)
return
element {$field/*/local-name()} {$field/*/text()}
}
</SubDocument>
}
</Document>
} </Documents>