How to get node without children in xQuery?

So, I have two nodes of elements to which I, in fact, try to join. I want the top level node to remain the same, but the child nodes are replaced by the ones referenced by the links.

Given:

<stuff> <item foo="foo" boo="1"/> <item foo="bar" boo="2" /> <item foo="baz" boo="3"/> <item foo="blah boo="4""/> </stuff> <list a="1" b="2"> <foo>bar</foo> <foo>baz</foo> </list> 

I want to skip the "list" and cross reference elements in "stuff" for this result:

 <list a="1" b="2"> <item foo="bar" boo="2" /> <item foo="baz" boo="3"/> </list> 

I want to do this without knowing what attributes may be in the "list". In other words, I do not want to explicitly call them, for example

 attribute a { $list/@a }, attribute b { $list/@b } 
+4
source share
2 answers

Application:

$list1/item[@foo = $list2/item/@foo]

This selects all the <item> elements in $list1 , the value of the foo attribute is equal to the foo attribute of one of the <item> elements in $ list2.

To copy all the attributes of the <list> element, do something like this :

  for $attr in /whateverIsthePathLeadingToList/list/@* return attibute {name($attr)} {$attr} 
+4
source

A little easier ... to create a new object from an existing one, but without its child attributes

suppose:

let $ old_list: =

This creates a new list copying its attributes.

  <list>{$old_list/@*}</list> 
+3
source

Source: https://habr.com/ru/post/1312603/


All Articles