[xquery-talk] xpath/xquery to eleminate certain nodes

John Snelson john.snelson at oracle.com
Tue Aug 26 13:00:50 PDT 2008


Hi Christian,

XQuery Update can be used to solve your problem fairly easily - 
something like this should do the trick:

copy $a := /
modify delete nodes $a/site/sites//categories/category[@name = 'hidden']
return $a

John

Christian Schlaefcke wrote:
> Hi,
> 
> I am fighting with a xpath/xquery expression to select the whole xml
> document except of certain nodes that I want to remove. The general
> structure of the document should be preserved.
> 
> With my approaches I either select too much or too less and my research @
> google & co. did not bring me any further :-(
> 
> The document structure is a bit tricky (arbitrarily nested categories) -
> imagine the following document:
> <sites>
>   <site>
>     <categories>
>       <category name="root">
>         <categories>
>           <category name="category_1">
>             <some>
>               <further>
>                 <nested>
>                   <info info_id="1">Important Information</info>
>                 </nested>
>               </further>
>             </some>
>           </category>
>           <category name="category_2">
>             <some>
>               <further>
>                 <nested>
>                   <info info_id="2">Less Important Information</info>
>                 </nested>
>               </further>
>             </some>
>           </category>
>         </categories>
>       </category>
>       <category name="hidden">
>         <categories>
>           <category name="dont_need_it">
>             <some>
>               <further>
>                 <nested>
>                   <info info_id="3">Unimportant Information</info>
>                 </nested>
>               </further>
>             </some>
>           </category>
>           <category name="dont_need_it_too">
>             <some>
>               <further>
>                 <nested>
>                   <info info_id="4">Even More Unimportant Information</info>
>                 </nested>
>               </further>
>             </some>
>           </category>
>         </categories>
>       </category>
>     </categories>
>   </site>
> </sites>
> 
> What I want would be:
> <sites>
>   <site>
>     <categories>
>       <category name="root">
>         <categories>
>           <category name="category_1">
>             <some>
>               <further>
>                 <nested>
>                   <info info_id="1">Important Information</info>
>                 </nested>
>               </further>
>             </some>
>           </category>
>         </categories>
>       </category>
>     </categories>
>   </site>
> </sites>
> 
> I tried several approaches like:
> /site/sites//categories/category[@name = 'category_1']
> or
> /site/sites//categories/category[@name != 'hidden']
> or
> /site/sites//categories/category/some/further/nested/info[@info_id = '1']
> 
> But all I get is something like this:
> <category name="category_1">
>   <some>
>     <further>
>       <nested>
>         <info info_id="1">Important Information</info>
>       </nested>
>     </further>
>   </some>
> </category>
> 
> What is missing to preserve the parent xml data?
> 
> Any hint that will point me to the right direction would be very appreciated!
> 
> Thank & Regards,
> 
> Christian
> 
> 
> 
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk


-- 
John Snelson, Oracle Corporation            http://snelson.org.uk/john
Berkeley DB XML:            http://oracle.com/database/berkeley-db/xml
XQilla:                                  http://xqilla.sourceforge.net


More information about the talk mailing list