dwarkesh
dwarkesh

Reputation: 1388

How to print "<" and ">" using XQuery?

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?

Upvotes: 3

Views: 2693

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

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/>

Upvotes: 3

tohuwawohu
tohuwawohu

Reputation: 13618

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 { ... }

Upvotes: 2

Related Questions