How to convert string to node in XQuery?

I would like to convert a string to node. I have a method that is defined as node, but I have a string (it is hardcoded). How to turn this string into node?

So, given the 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 to convert a string to node so that the method accepts the string.

+4
source share
4 answers

MarkLogic Solutions:

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

 xdmp:unquote($string). 

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

 xdmp:quote($node). 

Language agnostic solutions:

Node to line:

 fn:string($node) 
+8
source

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

 text { "your string goes here" } 

or if you prefer to create an element with the contents of a string, you can create an element something like this:

 element (some-element) { "your string goes here" } 
+8
source

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

+6
source

The answer to this question depends on which engine is used. For example, Saxon users, use the saxon:parse method.

The fact is that the XQuery specification has no built-in for this.

Generally speaking, you really will need to use this if you need to extract the embedded XML from the CDATA section. Otherwise, you can read files from the file system or declare XML directly.

For the most part, you should use a declarative form rather than a hard-coded string, for example. (using studio Stylus)

 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) 
+3
source

Source: https://habr.com/ru/post/1277073/


All Articles