0

I've got a question (the first one i actually ask on this website). I have a problem with my xquery declaration. What i am trying to do is to put a variable name inside a declaration of an xml tag (for exampl .

I don't know if that is even possible to do. I have been trying many syntaxes but that is what i got so far :

let $test:=  ("1", "2", "3")
return
<ROOT>
{
  for $x in $test return
    <TEST{$x}>foo</TEST{$x}>
}
</ROOT>

and that is what i want to have as a result :

<TEST1>foo</TEST1>
<TEST3>foo</TEST2>
<TEST3>foo</TEST3>

I know this is not how a xml should be done, that would be better to have something like :

<BAR>
  <TEST>foo</TEST>
  <ID>1</ID>
</BAR>><BAR>
  <TEST>foo</TEST>
  <ID>2</ID>
</BAR>

but i am not responsible for this part of the xml that is in input. The client is... Of course the xquery i wrote is a very simplified one from a bigger one and i just isolated the problem i got.

Is there any way to do this?

Cucunimbus
  • 5
  • 1
  • 4
  • As a matter of interest, when you "try many syntaxes" do you do this purely by trial and error, or do you go to some kind of reference documentation that tells you what the actual syntax of XQuery is? Because programming in any language by trial and error is never going to be a very efficient process... – Michael Kay Apr 14 '17 at 08:28
  • Possible duplicate of [XQuery: Create a new element with a given name?](http://stackoverflow.com/questions/2542235/xquery-create-a-new-element-with-a-given-name) – Jens Erat Apr 17 '17 at 12:42
  • @MichaelKay : I know but I don't know xquery very well. I am not the engineer responsible for that part but the one who was was on vacation. I had to find a way to do this and I didnt have the time to look into the documentation and the theory of xquery. I know it is not how things should be done but I just needed some hints on how to do it because I really didnt know where to start looking – Cucunimbus Apr 18 '17 at 13:47

1 Answers1

0

I found the solution :

let $test:=  ("1", "2", "3")

return
<ROOT>
{
  for $x in $test return
    element {concat('TEST', $x)} {'foo'}
}
</ROOT>
Cucunimbus
  • 5
  • 1
  • 4