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.

Vlad
  • 9,180
  • 5
  • 48
  • 67
Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
  • 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? – JeniT Sep 26 '08 at 08:45

5 Answers5

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)
Sixty4Bit
  • 12,852
  • 13
  • 48
  • 62
  • 1
    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. http://www.xml.com/lpt/a/1660 – Jim Burger Sep 23 '08 at 14:25
  • 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 () – Martin Probst Oct 20 '08 at 12:32
  • 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? – Sixty4Bit Mar 11 '09 at 22:30
  • It's a reasonable way to ask. You might also have asked how to parse (or de-serialize) a string into XML. – Eric Bloch Mar 06 '12 at 22:50
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" }
Sofia
  • 771
  • 1
  • 8
  • 22
9

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

mb21
  • 34,845
  • 8
  • 116
  • 142
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)
Sofia
  • 771
  • 1
  • 8
  • 22
Jim Burger
  • 4,399
  • 1
  • 24
  • 27
0

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

the hand of NOD
  • 1,639
  • 1
  • 18
  • 30