thichxai
thichxai

Reputation: 1147

Marklogic How to exclude multiple nodes in xml

I have syntax errors when exclude multiple nodes in element like "Sources" and "Navigators" nodes. But it works if I exclude only one node but not combine before returning the documents.

[(fn:local-name()  != ("Sources","Navigators")]

In Marklogic Qconsole:

  for $x in $uris
  let $doc := fn:doc($x)
  let $copymeta := <meta:Metadata> 
                    { $doc//meta:Metadata/*[(fn:local-name() != ("Sources","Navigators")] }
                   </meta:Metadata> 
  let $newxml := <omd:record>
                   { $copymeta }
                 </omd:record>
  return $newxml

Upvotes: 2

Views: 91

Answers (2)

BenW
BenW

Reputation: 433

The != operator has unintuitive semantics. See this previous question. When the code finds a *:Sources node, it evaluates as != to "Navigators", and when it finds a *:Navigators node, it evaluates as != to "Sources". And then you get all the nodes.

If you aren't comparing node sequences (so except is not an option), then instead of !=, you can use fn:not(A = B) to get the intended effect. In this case, fn:not(fn:local-name() = ("Sources","Navigators")) should work as you expect.

Upvotes: 2

grtjn
grtjn

Reputation: 20414

You have a open parenthesis too many in front of fn:local-name().

However, you could also make use of the except keyword, and a prefix wildcard. You would use it like this:

for $x in $uris
let $doc := fn:doc($x)
let $copymeta := <meta:Metadata> 
                   { $doc//meta:Metadata/(* except (*:Sources, *:Navigators)) }
                 </meta:Metadata> 
let $newxml := <omd:record>
                 { $copymeta }
               </omd:record>
return $newxml

HTH!

Upvotes: 1

Related Questions