3

For example

I am creating an element with dynamic data -

XQUERY
let $sttag:=  concat('<', '/')
return concat ($sttag, "newTag")

above XQuery returns output as &lt;/newTag instead of </newTag.

it there any way to print "<" and ">" using xquery?

tohuwawohu
  • 13,268
  • 4
  • 42
  • 61
dwarkesh
  • 1,378
  • 2
  • 10
  • 12

2 Answers2

3

Here is an illustration of the most dynamic way to construct an element -- the name isn't known at compile time and is generally different at every execution:

let $name := translate(concat('N', current-time()), ':','_')
  return
      element {$name} {}

produces:

<N21_26_40.708-07_00/>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
2

I recommend using XQuery's element constructors. You can create elements dynamically using a computed element constructor:

element book { 
    attribute isbn {"isbn-0060229357" }, 
    element title { "Harold and the Purple Crayon"},
    element author { 
        element first { "Crockett" }, 
        element last {"Johnson" }
    }
}

results in

<book isbn="isbn-0060229357">
    <title>Harold and the Purple Crayon</title>
    <author>
        <first>Crockett</first>
        <last>Johnson</last>
    </author>
</book>

(Example from the W3C XQuery specs)

In your case, you could use

element newTag { ... }
tohuwawohu
  • 13,268
  • 4
  • 42
  • 61
  • I'd just add that your `newTag` can also be dynamically constructed, e.g., `element { concat("new", "Tag") } { ... }`. – Joe Wicentowski Apr 20 '12 at 21:07
  • I'd also add that you could just put the XML elements right into the xQuery. I find it to be more readable than the 'element {}' syntax. I only use the 'element {}' syntax when I need to dynamically create an element. – Will C. Dec 20 '12 at 02:27