Reputation: 13402
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.
Upvotes: 5
Views: 28554
Reputation: 1789
you also can use fn:parse-xml(xs:string)
to convert your current valid XML string into a document.
Upvotes: 0
Reputation: 39508
If you are talking about strings that contain XML markup, there are standardized solutions (from XPath/XQuery Functions 3.0) as well:
Upvotes: 9
Reputation:
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" }
Upvotes: 10
Reputation: 4547
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)
Upvotes: 3
Reputation: 13402
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)
Upvotes: 11