[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

xpath/xquery to eleminate certain nodes

Michael Kay mike at saxonica.com
Tue Aug 26 12:56:52 PDT 2008


  xpath/xquery to eleminate certain nodes
This kind of operation is much easier in XSLT than in XQuery (unless you use
XQuery updates).

Any approach that simply tries to select nodes from the existing document is
going to fail: the nodes in your output have different children from the
nodes in the input, so they must be newly constructed nodes, they cannot be
nodes in the original document, or even copies of the nodes in the original
document.

In XSLT the general pattern is:

(a) write a template rule that copies each node unchanged, calling itself
recursively to copy its children

(b) override this with a more specific rule that applies to the nodes you
don't want to copy, and which does nothing when such nodes are encountered.

In XQuery you can simulate this with a recursive function that uses
typeswitch() to test what kind of node has been supplied as an argument. But
why fight the language? Use either XSLT, or XQuery Update.

Michael Kay
http://www.saxonica.com/
 

> -----Original Message-----
> From: http://x-query.com/mailman/listinfo/talk 
> [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Christian Schlaefcke
> Sent: 26 August 2008 10:54
> To: http://x-query.com/mailman/listinfo/talk
> Subject:  xpath/xquery to eleminate certain nodes
> 
> 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[@inf
> o_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
> 
> 
> 
> _______________________________________________
> http://x-query.com/mailman/listinfo/talk
> http://x-query.com/mailman/listinfo/talk



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.