0
votes

I am learning my way into an xquery database system. We are converting SQL databases to XML. A very handy idiom or hack to know about that I have not seen so far would be how to quickly run the equivalent of the dumb SQL query one often starts with. Given table named "account" and knowing it's reasonably sized, tell the database "select * from account;" just to see what happens.

With XQuery, that's not so easy to do. Has anybody settled on a recipe for this kind of thing, maybe something to do with getting local-name?

2
It makes a huge difference what your documents look like, and unfortunately optimization patterns tend differ across XQuery processors. - wst
..."table"? XQuery databases don't have tables at all. - Charles Duffy

2 Answers

1
votes

The XQuery equivalent to SELECT * from a SQL table is dumping a whole document. fn:doc() is the appropriate tool.

Thus, for a document named account.xml, you would select the entire document like so:

doc('account.xml')

...which may be, depending on your implementation and configuration details, something like:

doc('file://path/to/account.xml')

or

doc('http://yourcompany.com/your-database-namespace-root/account.xml')
0
votes

(: this 'xml flattener' function is inspired by 'Identity transform' example at https://en.wikipedia.org/wiki/Identity_transform#Using_XQuery :)

declare function local:copyChildElem2attrib ($element as element()) {
  element { node-name($element) }
    { $element/@*,
    for $child in $element/node()
    return if ($child instance of element())
      then
        attribute {  name($child) } { $child }
      else $child
  }
};

let $maxrecords := 150

let $myXtbl :=  doc('account.xml')

for $x at $pos in $myXtbl
where $pos < $maxrecords
return local:copyChildElem2attrib($x)