I'm not sure I follow what you're wanting exactly in the second result, but I'll take a stab at it: the first example below would produce your result #1 (I've put your original data in test.xml and assumed in your real data that 'child' and 'attr' might be repeated):
<root>{
for $child in doc('test.xml')/root/*
return
element {name($child)} {
for $attr at $index in $child/@*
return (
attribute {name($attr)} {$attr},
attribute {concat(name($attr), 2)} {$attr}
)
}
}</root>
It could be modified to put a different value in, like in result #2, like the below:
<root>{
for $child in doc('test.xml')/root/*
return
element {name($child)} {
for $attr at $index in $child/@*
return (
attribute {name($attr)} {$attr},
attribute {concat(name($attr), 2)} {
'**H**ello **W**orld'
}
)
}
}</root>
Hope that helps.