Cucunimbus
Cucunimbus

Reputation: 5

Insert variable name inside declaration of XML tag

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?

Upvotes: 0

Views: 581

Answers (1)

Cucunimbus
Cucunimbus

Reputation: 5

I found the solution :

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

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

Upvotes: 0

Related Questions