True - it's only a substitute for the one line in Erik's code snippet: 
$m/doc/node()[empty(self::b)]. You still need the document { <doc> .... } code 
to build the full structure.

Damon

________________________________________
From: [email protected] 
[[email protected]] On Behalf Of David Lee 
[[email protected]]
Sent: Thursday, June 14, 2012 2:21 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] How to exclude some elements from 
marklogic        xquery output?

Unfortuantely that doesnt return the <doc> element ...
Its more tricky to exclude children but maintain the parent.
let $m :=
  <doc>
    <a>a</a>
    <b>b</b>
  </doc>
return $m/node() except $m/b

Simply returns
  <a>a</a>

First, if you know how to do this in XSLT then ML 5.0+ supports XSLT so just 
use that.

For pure XQuery its a bit more complex in the general case (arbitrary deep 
elements).  FunctX might come to your rescue:

http://www.xqueryfunctions.com/xq/functx_remove-elements.html
-----

import module namespace functx = "http://www.functx.com"; at 
"/MarkLogic/functx/functx-1.0-nodoc-2007-01.xqy";

let $m :=
  <doc>
    <a>a</a>
    <b>b</b>
  </doc>
return functx:remove-elements($m,"b")

 Result:
<doc>
  <a>a</a>
</doc>






> -----Original Message-----
> From: [email protected] [mailto:general-
> [email protected]] On Behalf Of Damon Feldman
> Sent: Thursday, June 14, 2012 2:15 PM
> To: MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] How to exclude some elements from
> marklogic xquery output?
>
>
> For anyone not familiar with self:: it is an XPath axis
> (http://www.w3schools.com/xpath/xpath_axes.asp). Another common idiom
> for this is to use the "except" keyword;
>
> let $m :=
>   <doc>
>     <a>a</a>
>     <b>b</b>
>   </doc>
> return $m/node() except $m/b
>
>
> Yours,
> Damon
>
> -----Original Message-----
> From: [email protected] [mailto:general-
> [email protected]] On Behalf Of Erik Hennum
> Sent: Thursday, June 14, 2012 10:49 AM
> To: MarkLogic Developer Discussion
> Subject: Re: [MarkLogic Dev General] How to exclude some elements from
> marklogic xquery output?
>
> Hi, Karthik:
>
> Basically, you need to construct a new version of the doc root element
> that doesn't have the b child element but has all other child nodes.  If
> it is just a single exclusion, a simple predicate might be best:
>
> xquery version "1.0-ml";
> for $m in fn:doc()
> return document {
>     <doc>{
>         $m/doc/node()[empty(self::b)]
>     }</doc>
> }
>
> If you need to exclude lots of stuff, you're probably better off with 
> typeswitch:
>
> xquery version "1.0-ml";
> for $m in fn:doc()
> return document {
>     <doc>{
>         for $node in $m/doc/node()
>         return typeswitch($node)
>             case element(b) return ()
>             default return $node
>     }</doc>
> }
>
>
> Hoping that's useful,
>
>
> Erik Hennum
>
> ________________________________________
> From: [email protected] [general-
> [email protected]] On Behalf Of
> [email protected] [[email protected]]
> Sent: Thursday, June 14, 2012 7:25 AM
> To: [email protected]
> Subject: [MarkLogic Dev General] How to exclude some elements from
> marklogic    xquery output?
>
> Hi,
>
> I want to exclude some elements from xquery output..
> For ex: if I have many documents with the following xml ,
> <doc>
>                 <a>a</a>
>                 <b>b</b>
> </doc>
>
> I want to return all the documents, but element <b> must be excluded in all
> these documents.
>
> for $m in fn:doc()
> return $m
>
> Above xquery will return all the documents as it is. Can someone please let me
> know how to exclude only element <b> before returning the document. Is
> there any xquery or marklogic function readily available to ignore some
> elements while returning the response? I know that we can easily achieve this
> using XSLT. But, is it possible using Xquery or marklogic function?
>
>
> Thanks and Regards,
> Karthik
>
> This e-mail and any files transmitted with it are for the sole use of the
> intended recipient(s) and may contain confidential and privileged information.
> If you are not the intended recipient(s), please reply to the sender and 
> destroy
> all copies of the original message. Any unauthorized review, use, disclosure,
> dissemination, forwarding, printing or copying of this email, and/or any 
> action
> taken in reliance on the contents of this e-mail is strictly prohibited and 
> may
> be unlawful.
> _______________________________________________
> General mailing list
> [email protected]
> http://community.marklogic.com/mailman/listinfo/general
> _______________________________________________
> General mailing list
> [email protected]
> http://community.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://community.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://community.marklogic.com/mailman/listinfo/general

Reply via email to