TheForgot3n1
TheForgot3n1

Reputation: 57

Can't have several for loops inside an element construction XQuery

I am trying to construct a single node that "inverts" its attributes into child elements and child elements into attributes. For this, I need a for loop that goes through every attribute and promptly converts it into a node with the right name and right values, and vice versa. This is all fine and dandy, but understandably this code doesn't compile.

for $subele in //music/*
return
<doc>
{
  element {node-name($subele)} 
    {
    for $deepernode in $subele/* (: first for loop :)
    return 
    attribute {node-name($deepernode)} {concat("_",replace(lower-case($deepernode),"[^A-Za-z_0-9]","_"))}
    for $deepernode in $subele/@* (: second for loop :)
    return 
    element {node-name($deepernode)} {concat("_",replace(lower-case($deepernode),"[^A-Za-z_0-9]","_"))}

    }
 }
</doc>

Is there no other way of doing this, or do I need to first construct the attributes inversion, and then "modify" with the element, or vice versa?

Thank you very much Stack :)

Upvotes: 1

Views: 76

Answers (1)

TheForgot3n1
TheForgot3n1

Reputation: 57

So the answer was very simple, I tried variants of it in the wrong places initially. Oh well. Here it is:

  (for $deepernode in $subele/*
  return 
  attribute {node-name($deepernode)} {$deepernode/string()}),   
  (for $deepernode in $subele/@*
  return 
  element {node-name($deepernode)} {$deepernode/string()})

Put the for loops into a sequence, so to speak, by putting them both in paranthesis and writing ',' in between them. That's all.

PS: All the concat/replace stuff was unnecessary.

Upvotes: 1

Related Questions