How do you convert a string to a node in XQuery?
Asked Answered
I

5

5

I would like to convert a string into a node. I have a method that is defined to take a node, but the value I have is a string (it is hard coded). How do I turn that string into a node?

So, given an XQuery method:

define function foo($bar as node()*) as node() {
  (: unimportant details :)
}

I have a string that I want to pass to the foo method. How do I convert the string to a node so that the method will accept the string.

Indeterminable answered 23/9, 2008 at 14:12 Comment(1)
Does the string contain some escaped XML that you want to parse into a node, or do you just want to turn the string into a text node so that you can pass it to this particular function?Prostrate
I
11

MarkLogic solutions:

The best way to convert a string into a node is to use:

xdmp:unquote($string).

Conversely if you want to convert a node into a string you would use:

xdmp:quote($node).

Language agnostic solutions:

Node to string is:

fn:string($node)
Indeterminable answered 23/9, 2008 at 14:15 Comment(4)
xdmp:unquote and quote aren't part of the XQuery language spec, it is part of a set of MarkLogic extension libraries. Loading xml from string literals into a node depends on the XQuery engine being used. xml.com/lpt/a/1660Deborahdeborath
Also, you're node to string code probably doesn't do what the poster wants - it will only return the concatenated string value of all nodes below $node, but not the actual XML syntax (<foo/>)Weinman
I hate to accept my own answer, but this is what I was looking for and solved the problem I was having. Does the question need to be restated?Indeterminable
It's a reasonable way to ask. You might also have asked how to parse (or de-serialize) a string into XML.Electroanalysis
T
10

If you want to create a text node out of the string, just use a text node constructor:

text { "your string goes here" }

or if you prefer to create an element with the string content, you can construct an element something like this:

element (some-element) { "your string goes here" }
Terrel answered 9/11, 2008 at 21:47 Comment(0)
B
9

If you are talking about strings that contain XML markup, there are standardized solutions (from XPath/XQuery Functions 3.0) as well:

Bowsprit answered 2/8, 2012 at 19:36 Comment(0)
D
3

The answer to this question depends on what engine is being used. For instance, users of Saxon, use the saxon:parse method.

The fact is the XQuery spec doesn't have a built in for this.

Generally speaking you would only really need to use this if you needed to pull some embedded XML from a CDATA section. Otherwise you can read files in from the filesystem, or declare XML directly inline.

For the most you would use the declarative form, instead of a hardcoded string e.g. (using Stylus studio)

declare namespace my = "http://tempuri.org";

declare function my:foo($bar as node()*) as node() {
    <unimportant></unimportant>
} ;

let $bar := <node><child></child></node>

return my:foo(bar)
Deborahdeborath answered 23/9, 2008 at 14:43 Comment(0)
B
0

you also can use fn:parse-xml(xs:string) to convert your current valid XML string into a document.

Bogbean answered 9/6, 2021 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.